R z*_* zu 4 c++ gcc constructor c++17
在以下代码中,变量定义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’
}
Run Code Online (Sandbox Code Playgroud)
为什么它不会导致编译错误?
我用gcc 7.3 编译了代码g++ -std=c++17
当我使用gcc 7.3编译代码时g++ -std=c++14
,我收到一个错误.
我以为该行使用n
构造函数中的参数的默认值B
.
默认值n
是A()
,这是无法转换为一个int
.
我应该得到一个编译错误.但事实并非如此.
函数的类似情况(经测试foo
)将导致编译错误.