模板调用:未调用实际专业化

vij*_*vij 5 c++ string templates

#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
}
Run Code Online (Sandbox Code Playgroud)

为什么输出1而不是2?

Éri*_*ant 9

test<std::string> (注意:最后没有括号)会产生你期望的结果.

将其编写为test<std::string()>实例化模板,其类型为"函数不带参数并返回std :: string"