【发布时间】:2018-08-28 05:06:24
【问题描述】:
#include <iostream>
#include <string>
template <typename T>
void f(T x = std::string{""})
{
std::cout << '[' << x << ']' << std::endl;
}
int main()
{
f(23); // Case 1: Doesn't fail
f<int>(); // Case 2: Compilation error
f<int>(23); // Case 3: Doesn't fail
}
Case 1 和Case 3 不应该也失败吗,因为函数模板是由int 实例化的,并且默认值是std::string 类型的。
【问题讨论】:
标签: c++ c++11 templates language-lawyer default-parameters