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