我有这个功能:
template <typename T, T sep>
void split (){
std::cout << sep << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用此命令调用它时:split<'f'>();
我收到以下错误:
q3.cpp: In function ‘int main()’:
q3.cpp:36:16: error: no matching function for call to ‘split()’
split<'f'>();
^
q3.cpp:36:16: note: candidate is:
q3.cpp:31:6: note: template<class T, T sep> void split()
void split (){
^
q3.cpp:31:6: note: template argument deduction/substitution failed:
Run Code Online (Sandbox Code Playgroud)
为什么?
为什么?
因为第一个模板参数是类型,而不是值.'f'是字符常量,值.你不能插入一个类型.
一个正确的电话会split<char, 'f'>().
在即将推出的C++ 17标准中,您实际上可以以允许所需语法的方式重新定义模板:
template <auto sep>
void split (){
std::cout << sep << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
现在,呼叫split<'f'>()将推断出类型sep.
| 归档时间: |
|
| 查看次数: |
40 次 |
| 最近记录: |