#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”