prog.cpp:9:13: error: ‘result_type’ does not name a type
prog.cpp:9:13: note: (perhaps ‘typename std::unary_function<_Arg, _Result>::result_type’ was intended)
Compiler: http://ideone.com/vttG8W
Why cannot I use result_type directly?
#include <functional>
using namespace std;
template <typename ARGUEMENT, typename RESULT>
class FunctorBase : public std::unary_function<ARGUEMENT, RESULT>
{
public:
virtual result_type operator () (argument_type) = 0;
FunctorBase() {}
virtual ~FunctorBase() {}
};
int main()
{
FunctorBase<int&, void>();
}
Because it is an unqualified name.
When you use unqualified names in a class template, you have to tell the compiler that it should not immediately search for global names during the first phase of the two-phase name lookup, but rather wait until instantiation (because the name could come from a base class, as is the case here).
In this case, you could do this (and make result_type a dependent, qualified name):
typedef typename std::unary_function<ARGUEMENT, RESULT>::result_type result_type;
Notice that the same applies to argument_type.
typedef typename std::unary_function<ARGUEMENT, RESULT>::argument_type argument_type;
Armed with those two typedefs, the original member function declaration will now compile:
virtual result_type operator () (argument_type) = 0;
Because result_type and argument_type depend on the template parameters. Use:
virtual typename std::unary_function<ARGUEMENT, RESULT>::result_type
operator () (typename std::unary_function<ARGUEMENT, RESULT>::argument_type) = 0;
or, if you need it in more places, add
using typename std::unary_function<ARGUEMENT, RESULT>::result_type;
using typename std::unary_function<ARGUEMENT, RESULT>::argument_type;
in the beginning of your class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With