#include <iostream>
using namespace std;
template<typename T>
void test() {
cout << "1";
}
template<>
void test<std::string>() {
cout << "2";
}
int main() {
test<std::string()>(); //expected output 2 but actual output 1
}
Why is the output 1 and not 2?
test<std::string>
(note: no parentheses at the end) would yield what you expect.
Writing it as test<std::string()>
instantiates the template with the type "function taking no arguments and returning std::string"
Did you mean to invoke the function like: test<std::string>()
?
In your test<std::string()>()
, the template parameter is not std::string
but a function type (a function taking no arguments and returning std::string
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With