Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using directive does not work in template function object.

Reply
Thread Tools

using directive does not work in template function object.

 
 
jimking2000@gmail.com
Guest
Posts: n/a
 
      04-09-2009
Hello everyone

I can't compile the following code.

compiler: g++ 4.3.2
OS: openSUSE 11



#include <functional>
#include <algorithm>
#include <iostream>
#include <string>
#include <memory>
#include <vector>


using namespace std;


class test
{
public:
void func(int i) { wcout << i << L": test\n"; }
};


namespace
{
template<class T>
class destroyer : public unary_function<T, void>
{
public:
//using typename unary_function<T, void>::result_type;
using typename unary_function<T, void>::argument_type;


public:
typename unary_function<T, void>::result_type
operator()(/*typename unary_function<T, void>::*/argument_type
pointer) const // compile error here
{
delete pointer;
}
};
}


int main()
{
typedef vector<test *> vectest;
vectest data;

for (int i = 0; i < 5; ++i)
data.push_back(new test);

for_each(data.begin(), data.end(), bind2nd(mem_fun(&test::func), 0));

for_each(data.begin(), data.end(), destroyer<vectest::value_type>());

return 0;
}


The error message is:
make all
Building file: ../src/Test_Cplusplus.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/
Test_Cplusplus.d" -MT"src/Test_Cplusplus.d" -o"src/Test_Cplusplus.o"
"../src/Test_Cplusplus.cpp"
.../src/Test_Cplusplus.cpp:33: error: ‘argument_type’ is not a type
/usr/include/c++/4.3/bits/stl_algo.h: In function ‘_Funct std::for_each
(_IIter, _IIter, _Funct) [with _IIter =
__gnu_cxx::__normal_iterator<test**, std::vector<test*,
std::allocator<test*> > >, _Funct = <unnamed>::destroyer<test*>]’:
.../src/Test_Cplusplus.cpp:51: instantiated from here
/usr/include/c++/4.3/bits/stl_algo.h:3791: error: invalid conversion
from ‘test*’ to ‘int’
/usr/include/c++/4.3/bits/stl_algo.h:3791: error: initializing
argument 1 of ‘typename std::unary_function<T,
void>::result_type<unnamed>::destroyer<T>:perato r()(int) const [with
T = test*]’
.../src/Test_Cplusplus.cpp: In member function ‘typename
std::unary_function<T,
void>::result_type<unnamed>::destroyer<T>:perato r()(int) const [with
T = test*]’:
/usr/include/c++/4.3/bits/stl_algo.h:3791: instantiated from ‘_Funct
std::for_each(_IIter, _IIter, _Funct) [with _IIter =
__gnu_cxx::__normal_iterator<test**, std::vector<test*,
std::allocator<test*> > >, _Funct = <unnamed>::destroyer<test*>]’
.../src/Test_Cplusplus.cpp:51: instantiated from here
.../src/Test_Cplusplus.cpp:35: error: type ‘int’ argument given to
‘delete’, expected pointer
make: *** [src/Test_Cplusplus.o] error 1

So, here, the using directive cannot introduce the argument_type of
the base template.

While I can use base::argument_type directly.
typename unary_function<T, void>::result_type
operator()(typename unary_function<T, void>::argument_type pointer)
const // OK

Does this behavior conform to the standard?

Thanks
Jim
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-09-2009
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> Hello everyone
>
> I can't compile the following code.
>
> compiler: g++ 4.3.2
> OS: openSUSE 11
>
>
>
> #include <functional>
> #include <algorithm>
> #include <iostream>
> #include <string>
> #include <memory>
> #include <vector>
>
>
> using namespace std;
>
>
> class test
> {
> public:
> void func(int i) { wcout << i << L": test\n"; }
> };
>
>
> namespace
> {
> template<class T>
> class destroyer : public unary_function<T, void>
> {
> public:
> //using typename unary_function<T, void>::result_type;
> using typename unary_function<T, void>::argument_type;
>
>
> public:
> typename unary_function<T, void>::result_type
> operator()(/*typename unary_function<T, void>::*/argument_type
> pointer) const // compile error here
> {
> delete pointer;
> }
> };
> }
>
>
> int main()
> {
> typedef vector<test *> vectest;
> vectest data;
>
> for (int i = 0; i < 5; ++i)
> data.push_back(new test);
>
> for_each(data.begin(), data.end(), bind2nd(mem_fun(&test::func), 0));
>
> for_each(data.begin(), data.end(), destroyer<vectest::value_type>());
>
> return 0;
> }
>
>
> The error message is:
> make all
> Building file: ../src/Test_Cplusplus.cpp
> Invoking: GCC C++ Compiler
> g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/
> Test_Cplusplus.d" -MT"src/Test_Cplusplus.d" -o"src/Test_Cplusplus.o"
> "../src/Test_Cplusplus.cpp"
> ../src/Test_Cplusplus.cpp:33: error: ‘argument_type’ is not a type
> /usr/include/c++/4.3/bits/stl_algo.h: In function ‘_Funct std::for_each


[snip]

> Does this behavior conform to the standard?


Not as far as I know. E.g. the code compiles fine with Comeau Online, and I
don't know any rule of the standard that would make it ill-formed. However, the
common technique for making such code work with g++ is to use a 'typedef'
instead of a 'using'-declaration; it's so common that I actually /believed/ it
was probably required by the standard, but checking I can find no such rule.

You might consider defining a macro like

#define USING_BASE_TYPE( name ) typedef Base::name name;

I just wish there was some less visually imposing convention for macro names.

It might at first glance seem as if this macro would be of little value, for
what about multiple inheritance?

However, in practice the types you need mainly come from a single "main" base class.

The practical upshot is that with regard to template handling the standard is
not a document to be relied on, because every compiler, except possibly Comeau,
has its own special quirks, sort of like Internet Explorer wrt. HTML.

So the only way to do things portably is to test, test, test, with different
compilers, and adopt coding conventions like the macro above that work
cross-compiler.

By the way, regarding cross-compiler portable code, note that std::wcout is not
supported by MinGW g++ for Windows... That is, with a default build of that
compiler, such as the binary that most people install. Given that the standard
library's wide streams are just so much baggage, of negative real value
considering the complexity they add via templating of stream functionality, I
think that's entirely reasonable -- so, Just Say No to the wide streams!


Cheers & hth.,

- Alf

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! Just going there is good. Linking
to it is even better! Thanks in advance!
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-09-2009
* Alf P. Steinbach:
> * (E-Mail Removed):
>> Hello everyone
>>
>> I can't compile the following code.
>>
>> compiler: g++ 4.3.2
>> OS: openSUSE 11
>>
>>
>>
>> #include <functional>
>> #include <algorithm>
>> #include <iostream>
>> #include <string>
>> #include <memory>
>> #include <vector>
>>
>>
>> using namespace std;
>>
>>
>> class test
>> {
>> public:
>> void func(int i) { wcout << i << L": test\n"; }
>> };
>>
>>
>> namespace
>> {
>> template<class T>
>> class destroyer : public unary_function<T, void>
>> {
>> public:
>> //using typename unary_function<T, void>::result_type;
>> using typename unary_function<T, void>::argument_type;
>>
>>
>> public:
>> typename unary_function<T, void>::result_type
>> operator()(/*typename unary_function<T, void>::*/argument_type
>> pointer) const // compile error here
>> {
>> delete pointer;
>> }
>> };
>> }
>>
>>
>> int main()
>> {
>> typedef vector<test *> vectest;
>> vectest data;
>>
>> for (int i = 0; i < 5; ++i)
>> data.push_back(new test);
>>
>> for_each(data.begin(), data.end(), bind2nd(mem_fun(&test::func), 0));
>>
>> for_each(data.begin(), data.end(), destroyer<vectest::value_type>());
>>
>> return 0;
>> }
>>
>>
>> The error message is:
>> make all
>> Building file: ../src/Test_Cplusplus.cpp
>> Invoking: GCC C++ Compiler
>> g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/
>> Test_Cplusplus.d" -MT"src/Test_Cplusplus.d" -o"src/Test_Cplusplus.o"
>> "../src/Test_Cplusplus.cpp"
>> ../src/Test_Cplusplus.cpp:33: error: ‘argument_type’ is not a type
>> /usr/include/c++/4.3/bits/stl_algo.h: In function ‘_Funct std::for_each

>
> [snip]
>
>> Does this behavior conform to the standard?

>
> Not as far as I know. E.g. the code compiles fine with Comeau Online,


That should of course be "As far as I know".

Grr.


> and I don't know any rule of the standard that would make it ill-formed.
> However, the common technique for making such code work with g++ is to
> use a 'typedef' instead of a 'using'-declaration; it's so common that I
> actually /believed/ it was probably required by the standard, but
> checking I can find no such rule.
>
> You might consider defining a macro like
>
> #define USING_BASE_TYPE( name ) typedef Base::name name;
>
> I just wish there was some less visually imposing convention for macro
> names.
>
> It might at first glance seem as if this macro would be of little value,
> for what about multiple inheritance?
>
> However, in practice the types you need mainly come from a single "main"
> base class.
>
> The practical upshot is that with regard to template handling the
> standard is not a document to be relied on, because every compiler,
> except possibly Comeau, has its own special quirks, sort of like
> Internet Explorer wrt. HTML.
>
> So the only way to do things portably is to test, test, test, with
> different compilers, and adopt coding conventions like the macro above
> that work cross-compiler.
>
> By the way, regarding cross-compiler portable code, note that std::wcout
> is not supported by MinGW g++ for Windows... That is, with a default
> build of that compiler, such as the binary that most people install.
> Given that the standard library's wide streams are just so much baggage,
> of negative real value considering the complexity they add via
> templating of stream functionality, I think that's entirely reasonable
> -- so, Just Say No to the wide streams!
>
>
> Cheers & hth.,
>
> - Alf
>



--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! Just going there is good. Linking
to it is even better! Thanks in advance!
 
Reply With Quote
 
jimking2000@gmail.com
Guest
Posts: n/a
 
      04-09-2009
On Apr 9, 12:05*pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * Alf P. Steinbach:
>
>
>
> > * (E-Mail Removed):
> >> Hello everyone

>
> >> I can't compile the following code.

>
> >> compiler: g++ 4.3.2
> >> OS: openSUSE 11

>
> >> #include <functional>
> >> #include <algorithm>
> >> #include <iostream>
> >> #include <string>
> >> #include <memory>
> >> #include <vector>

>
> >> using namespace std;

>
> >> class test
> >> {
> >> public:
> >> *void func(int i) { wcout << i << L": test\n"; }
> >> };

>
> >> namespace
> >> {
> >> *template<class T>
> >> *class destroyer : public unary_function<T, void>
> >> *{
> >> *public:
> >> * * *//using typename unary_function<T, void>::result_type;
> >> * using typename unary_function<T, void>::argument_type;

>
> >> *public:
> >> * * *typename unary_function<T, void>::result_type
> >> * * *operator()(/*typename unary_function<T, void>::*/argument_type
> >> pointer) const // compile error here
> >> * {
> >> * *delete pointer;
> >> * }
> >> *};
> >> }

>
> >> int main()
> >> {
> >> * * typedef vector<test *> vectest;
> >> * * vectest data;

>
> >> *for (int i = 0; i < 5; ++i)
> >> * data.push_back(new test);

>
> >> *for_each(data.begin(), data.end(), bind2nd(mem_fun(&test::func), 0));

>
> >> *for_each(data.begin(), data.end(), destroyer<vectest::value_type>());

>
> >> * * return 0;
> >> }

>
> >> The error message is:
> >> make all
> >> Building file: ../src/Test_Cplusplus.cpp
> >> Invoking: GCC C++ Compiler
> >> g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/
> >> Test_Cplusplus.d" -MT"src/Test_Cplusplus.d" -o"src/Test_Cplusplus.o"
> >> "../src/Test_Cplusplus.cpp"
> >> ../src/Test_Cplusplus.cpp:33: error: argument_type is not a type
> >> /usr/include/c++/4.3/bits/stl_algo.h: In function _Funct std::for_each

>
> > [snip]

>
> >> Does this behavior conform to the standard?

>
> > Not as far as I know. E.g. the code compiles fine with Comeau Online,

>
> That should of course be "As far as I know".
>
> Grr.
>
>
>
> > and I don't know any rule of the standard that would make it ill-formed..
> > However, the common technique for making such code work with g++ is to
> > use a 'typedef' instead of a 'using'-declaration; it's so common that I
> > actually /believed/ it was probably required by the standard, but
> > checking I can find no such rule.

>
> > You might consider defining a macro like

>
> > * #define USING_BASE_TYPE( name ) typedef Base::name name;

>
> > I just wish there was some less visually imposing convention for macro
> > names.

>
> > It might at first glance seem as if this macro would be of little value,
> > for what about multiple inheritance?

>
> > However, in practice the types you need mainly come from a single "main"
> > base class.

>
> > The practical upshot is that with regard to template handling the
> > standard is not a document to be relied on, because every compiler,
> > except possibly Comeau, has its own special quirks, sort of like
> > Internet Explorer wrt. HTML.

>
> > So the only way to do things portably is to test, test, test, with
> > different compilers, and adopt coding conventions like the macro above
> > that work cross-compiler.

>
> > By the way, regarding cross-compiler portable code, note that std::wcout
> > is not supported by MinGW g++ for Windows... That is, with a default
> > build of that compiler, such as the binary that most people install.
> > Given that the standard library's wide streams are just so much baggage,
> > of negative real value considering the complexity they add via
> > templating of stream functionality, I think that's entirely reasonable *
> > -- *so, Just Say No to the wide streams!

>
> > Cheers & hth.,

>
> > - Alf

>
> --
> Due to hosting requirements I need visits to <url:http://alfps.izfree.com/>.
> No ads, and there is some C++ stuff! Just going there is good. Linking
> to it is even better! Thanks in advance!


Hi Alf

Thanks for your reply.

I'd like to write code like this:

class destroyer : public unary_function<T, void>
{
public:
result_type operator()(argument_type pointer) const
{
...
}
};

instead of this:

class destroyer : public unary_function<T, void>
{
public:
void operator()(T pointer) const
{
...
}
};

Because if I want to change unary_function<T, void> to
unary_function<T, int>, I need revise only one place.

But the customized function object cannot be a template, since the
template rule is complex and odd. Until I know the using directive, I
can keep my coding style with template. What baffles me is it is still
not portable!!!

So maybe it's time to change my coding style. Althought I wish gcc
provides the functionality to let the first test case run.

Regards,
Jim King
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointer as template argument - does not work for function template avasilev C++ 4 12-22-2011 02:11 PM
Template function within class template not not resolving when passedfunction local class? jrwats C++ 1 03-01-2010 08:30 AM
template function within class template not not resolving? jrwats C++ 0 02-28-2010 06:12 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
The 'Location' attribute is not supported by the 'OutPutCache' directive????when i set in the directive .NET Follower ASP .Net 1 02-13-2004 10:32 AM



Advertisments