【发布时间】:2017-07-23 08:49:03
【问题描述】:
我在 mac 上使用 clang 并且有:
$cat my.cpp
#include<type_traits>
using namespace std;
template<int i>
struct place_holder{};
place_holder<1> _1;
place_holder<2> _2;
template<typename T>
struct is_placeholder:public integral_constant<int,0>{};
template<int i>
struct is_placeholder<place_holder<i> >:public integral_constant<int,i>{};
template<typename T>
int check(T&& t){
return is_placeholder<t>()::value;
}
int main(){
int i=check<_1>();
return 0;
}
最后一行编译失败:
my.cpp:13:27: error: template argument for template type parameter must be a type
return is_placeholder<t>()::value;
^
my.cpp:7:19: note: template parameter is declared here
template<typename T>
^
my.cpp:16:11: error: no matching function for call to 'check'
int i=check<_1>();
^~~~~~~~~
my.cpp:12:5: note: candidate function template not viable: requires single argument 't', but no arguments were
provided
int check(T&& t){
^
2 errors generated.
真的很奇怪,我的“struct is_placeholder”真的是一个模板,对吧?为什么说不是?
如何纠正我的程序?谢谢!
【问题讨论】:
-
minimal reproducible example,拜托。该错误强烈暗示您没有编译您显示的代码。
-
它应该编译:godbolt.org/g/55Vud3
-
刚刚编辑。谢谢!
标签: c++ templates struct specialization explicit