Topic: function objects inside functions


Author: "Marco Manfredini" <marco@technoboredom.net>
Date: Fri, 8 Dec 2000 15:54:00 GMT
Raw View


"Juan Vali=F1o" <juan.vali@teleline.es> wrote in message
news:VShT5.26433$k06.626142@telenews.teleline.es...
> I try to use a function-object near it use:
>
> void foo() {
>     struct Print{ void operator()(int x) { cout << x; } };
>     for_each(aVector.begin(), aVector.end(), Print());
> };
>
> C++Builder refuses it unless I put the Print declaration out of foo.
>
> Is this compiler ok?, In that case, is there any solution (without
template
> metaprogramming)?.


void foo() {
     struct
nt{=20
        static void print()(int x) { cout << x; }=20
     };
     for_each(aVector.begin(), aVector.end(), Print::print);
 };

But sometime, one don't want to call statics, since one want to bind loca=
ls. So if you need an instance then try this:=20

template<class T>
struct localfun
{
 localfun &that;=20
 virtual void operator () (T x) { that(x); };=20
 localfun() : that(*this) {}
};=20

void foo(vector<int> &x)
{
 struct Print : localfun<int> {=20
  void operator() (int x) { cout << x << endl;  }=20
 };

 for_each(x.begin(), x.end(), Print().that);=20
}

Hehehe!! This works, "that" always points to the original "Print" and wil=
l be shared on copy-construction.=20

--=20
-- Marco

dig @138.195.138.195 goret.org. axfr | grep '^c..\..*A' | sort | cut -b5-=
36
| perl -e 'while(<>){print pack("H32",$_)}' | gzip -d




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Juan Vali o" <juan.vali@teleline.es>
Date: 2000/11/27
Raw View
I try to use a function-object near it use:

void foo() {
    struct Print{ void operator()(int x) { cout << x; } };
    for_each(aVector.begin(), aVector.end(), Print());
};

C++Builder refuses it unless I put the Print declaration out of foo.

Is this compiler ok?, In that case, is there any solution (without template
metaprogramming)?.

Thanks.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: firstian@nospam.bellatlantic.net (Joe Chan)
Date: 2000/11/27
Raw View
What about:

void foo() {
    struct Print { void operator()(int x) { cout << x; } } aPrint;
    for_each(aVector.begin(), aVector.end(), aPrint );
}

?

Juan Vali=F1o <juan.vali@teleline.es> wrote:

> I try to use a function-object near it use:
>=20
> void foo() {
>     struct Print{ void operator()(int x) { cout << x; } };
>     for_each(aVector.begin(), aVector.end(), Print());
> };
>=20
> C++Builder refuses it unless I put the Print declaration out of foo.
>=20
> Is this compiler ok?, In that case, is there any solution (without temp=
late
> metaprogramming)?.
>=20
> Thanks.
>=20
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with=
 ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu   =
 ]
> [              --- Please see the FAQ before posting. ---              =
 ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html               =
 ]
> [ Note that the FAQ URL has changed!  Please update your bookmarks.    =
 ]


--=20
Joe Chan

Remove "nospam" to get my address.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: vaps4bm@prism.gatech.edu (Brian McNamara!)
Date: 2000/11/27
Raw View
"Juan Vali   o" <juan.vali@teleline.es> once said:
>I try to use a function-object near it use:

>void foo() {
>    struct Print{ void operator()(int x) { cout << x; } };
>    for_each(aVector.begin(), aVector.end(), Print());
>};

>C++Builder refuses it unless I put the Print declaration out of foo.

>Is this compiler ok?, In that case, is there any solution (without template
>metaprogramming)?.

The compiler is right; the code is illegal.  Template types need
external linkage.

The solution, as you said, is just to define Print outside the function.

--
Brian McNamara

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Tom <the_wid@my-deja.com>
Date: 2000/11/27
Raw View
Previously, Juan Vali wrote in comp.std.c++:
> I try to use a function-object near it use:
>
> void foo() {
>     struct Print{ void operator()(int x) { cout << x; } };
>     for_each(aVector.begin(), aVector.end(), Print());
> };
>
> C++Builder refuses it unless I put the Print declaration out of foo.
>
> Is this compiler ok?, In that case, is there any solution (without template
> metaprogramming)?.

I think this is normal - AFAIK template parameters must have external linkage, which local structs don't have.

How about:

copy(aVector.begin(), aVector.end(), ostream_iterator<int>(cout, ""));

Tom

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]