Reputation: 691
I've this function:
template <typename T, T sep>
void split (){
std::cout << sep << std::endl;
}
When I try to call it with this command: split<'f'>();
I get the following error:
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:
Why?
Upvotes: 1
Views: 52
Reputation: 170055
Why?
Because the first template parameter is a type, not a value. 'f'
is character constant, a value. And you cannot plug it in for a type.
A correct call would be split<char, 'f'>()
.
In the upcoming C++17 standard, you can in fact redefine your template in a way that allows the syntax you want:
template <auto sep>
void split (){
std::cout << sep << std::endl;
}
Now the call split<'f'>()
will deduce the type of sep
.
Upvotes: 8