using directive does not work in template function object.

J

jimking2000

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>::eek:perator()(int) const [with
T = test*]’
.../src/Test_Cplusplus.cpp: In member function ‘typename
std::unary_function<T,
void>::result_type<unnamed>::destroyer<T>::eek:perator()(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
 
A

Alf P. Steinbach

* (e-mail address 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
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* (e-mail address 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.
 
J

jimking2000

* Alf P. Steinbach:


* (e-mail address 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
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.,

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top