具有错误类型默认值的构造函数不会引发GCC 7错误

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.

默认值nA(),这是无法转换为一个int.

我应该得到一个编译错误.但事实并非如此.

函数的类似情况(经测试foo)将导致编译错误.

hlt*_*hlt 6

你遇到了GCC bug#83020.Clang 6和GCC 8(以及GCC 6)正确拒绝您的示例代码,C++ 14模式下的GCC 7也是如此.