c++ - 模板调用 : Actual specialization not called

标签 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
}

为什么输出是1而不是2?

最佳答案

test<std::string> (注意:末尾没有括号)会产生您期望的结果。

写成test<std::string()>用“不带参数并返回 std::string 的函数”类型实例化模板

关于c++ - 模板调用 : Actual specialization not called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6075794/

相关文章:

从数组维度推导 C++ 模板参数

c++ - 可以将动态值传递给模板

C++ 模板字符串连接

c++ - 从包含特定字符串的行开始读取

关于比较器的 C++ 模板问题

c++ - boost-asio 作为守护进程的 "template"

c++ - 如何从 C++ DLL 导出函数并在 Delphi 中使用?

java - 为字符串中的每个字符分配一组新字符

c++ - 受条件限制的模板特化

c++ - 模板和接口(interface)的多重继承问题