Re: using directive does not work in template function object.
* jimking2000@gmail.com:
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!