dependent-name T被解析为非类型,但实例化会产生一个类型

时间:2016-07-08 09:17:42

标签: c++ c++11 metaprogramming template-meta-programming

我意识到有一些问题涉及到这个问题,但我还没有找到解决方案。

我想只在函数f的所有参数都是pod时启用它。

我有以下代码来执行此操作:

#include <type_traits>

template <typename... Conds>
struct and_ : std::true_type
{
};

template <typename Cond, typename... Conds>
struct and_<Cond, Conds...> :
    std::conditional<Cond::value, and_<Conds...>, std::false_type>::type
{
};

template <typename... T>
using are_all_pod = and_<std::is_pod<T>...>;

template<typename... Args,
         typename = typename std::enable_if<are_all_pod<Args...>::type>>
void f(Args ... args)
{
}

int main()
{
  f(1,2);
}

我收到以下错误:

main.cpp: In function ‘int main()’:
main.cpp:25:8: error: no matching function for call to ‘f(int, int)’
   f(1,2);
        ^
main.cpp:19:6: note: candidate: template<class ... Args, class> void f(Args ...)
 void f(Args ... args)
      ^
main.cpp:19:6: note:   template argument deduction/substitution failed:
main.cpp:18:10: error: dependent-name ‘and_<std::is_pod<T>...>::type’ is parsed as a non-type, but instantiation yields a type
          typename = typename std::enable_if<are_all_pod<Args...>::type>>
          ^
main.cpp:18:10: note: say ‘typename and_<std::is_pod<T>...>::type’ if a type is meant

我尝试了以下编译器的建议但是我得到了同样的错误。使用gcc 5.2.1

1 个答案:

答案 0 :(得分:2)

std::enable_if<are_all_pod<Args...>::type>毫无意义。

将其更改为std::enable_if<are_all_pod<Args...>::value, void>::type,程序将编译。