延迟crtp基类中的成员函数实例化

时间:2012-06-10 02:43:45

标签: c++ c++11 visual-studio-2012

目前,我有一个CRTP基类,它使用traits类来确定它的成员函数的返回类型。我一直在玩C ++ 11,并且有以下代码,它们不需要traits类,但需要默认的函数模板参数。有没有办法修改它在Visual Studio 2012中工作,它不支持C ++ 11的这个功能?

#include <iostream>

using namespace std;

template<typename T, typename Ignore> 
struct ignore { typedef T type; };

template<typename T> 
struct A 
{
    template<class IgnoredParam = void>
    auto foo() -> decltype(declval<typename ignore<T*, IgnoredParam>::type >()->foo_impl()) 
    {
        return static_cast<T*>(this)->foo_impl();
    }        
};

struct B : public A<B> 
{
    int foo_impl() { return 0;}

};

int main()
{
    B b;
    int i = b.foo();
    cout << i << '\n';
}

1 个答案:

答案 0 :(得分:0)

您只需使用

即可
decltype(declval<T>()->foo_impl())

作为延迟返回类型。

相关问题