Reputation: 891
I'm trying to typedef
the return type of a member function of a template argument. So something like this:
template <typename T>
class DoSomething{
typedef return_type (T::*x)(void)const data_type;
data_type x1;
};
In this case, I want to typedef
the return type of a const member function of the template parameter, called x
. In this example return_type
is just a place holder, just to show what I want. It won't compile.
EDIT:
The reason I want it this way is: I want to do the operations on x1 or any other variable of type data_type
in this example, with the same precision as the return type of the x() member function. So if x() returns a float all my template ops will be in floats and so on.
I found one answer on SO. But I don't want to use any C++11 features. So no decltype
, no auto
. Just something that will work with a C++03 compiler.
Upvotes: 2
Views: 332
Reputation: 72271
This seems to work (even when Boost Typeof is forced to use the long way rather than any C++11 or compiler-specific feature):
#include <boost/utility/declval.hpp>
#include <boost/typeof/typeof.hpp>
template <typename T>
class DoSomething {
typedef BOOST_TYPEOF_TPL(boost::declval<T const&>().x()) data_type;
data_type x1;
};
But the resulting type cannot involve any class, struct, union, or enum types.
Upvotes: 2
Reputation: 10557
The core idea of the typedef is the following:
typedef known_type [<modifiers>] new_type;
In this case known_type
should be already defined and known. Dot. There is not other way. Based on this known existing type you can define a new type.
Start thinking from this.
Upvotes: 0
Reputation: 86
The easiest way to do this may be boost's typeof, which operates like decltype, but works in C++03.
Upvotes: 1