简体   繁体   中英

typedef return type of template parameter's member function

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.

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.

最简单的方法可能是boost的typeof,其操作类似于decltype,但在C ++ 03中有效。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM