在以下代码中,变量定义
B<int, int>(14);
应为错误:
#include <iostream>
struct A {
};
template<class T, class R=A>
class B {
public:
explicit B(const int s, R n = A()) {
std::cout << "c\n";
}
};
template <class T, class R=A>
void foo(const int s, R nx = A()) {};
int main() {
B<int, int>(14);
// foo<int, int>(14);
// error: could not convert âA()â from âAâ to âintâ
}
为什么它不会导致编译错误?
我
compiled
gcc 7.3和
g++ -std=c++17
当我用gcc 7.3和
g++ -std=c++14
,我得到一个错误。
我认为该行使用参数的默认值
n
在构造函数中
B
。
的默认值
N
是
A()
,不能转换为
int
。
我应该得到一个编译错误。但事实并非如此。
功能的类似情况(经测试
foo
)将导致编译错误。