#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
}
为什么输出是1而不是2?
最佳答案
test<std::string>
(注意:末尾没有括号)会产生您期望的结果。
写成test<std::string()>
用“不带参数并返回 std::string 的函数”类型实例化模板
关于c++ - 模板调用 : Actual specialization not called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6075794/