小编Pet*_*ome的帖子

访问类型成员

在我的例子中,我有一个班级Foo<T>.在我的函数中,test我需要获取Foo正常类型的模板参数.首先我开始使用std::conditional但忘记了模板参数必须全部有效,无论选择哪一个.是为non-Foo类型创建类型特化的唯一方法吗?

#include <type_traits>

template <typename TYPE>
class Foo
{
public:
  using M = TYPE;
};

template <typename T>
void test(const T& a)
{
  // actually I would have used !is_foo<T>::value for the first arg
  // but this check is fine to minimise the example
  using MY_TYPE = typename std::conditional<
    std::is_same<T, int>::value,
    T,
    typename T::M>::type; // <---Error: error: type 'int' cannot be used prior to '::' because it has no …
Run Code Online (Sandbox Code Playgroud)

c++ type-traits template-meta-programming c++11

2
推荐指数
1
解决办法
113
查看次数