模板函数可以使用带有删除的构造函数的声明类型



,我知道不允许使用已删除的构造函数:

struct no_def
{
    no_def() = delete;
};
void test()
{
    decltype(no_def()) a{}; //error: use of deleted function ‘no_def::no_def()’
}

但是,如果我制作模板"测试"功能,它将编译

template<typename...>
void test()
{
    decltype(no_def()) a{}; //OK
}

,它也

template<typename...>
void test()
{
    decltype(no_def("not", "defined", "constructor")) a{}; //OK
}

有人可以解释吗?

这显然是GCC中的一个错误。最新的叮当声和最新的Visual C 正确打印了诊断消息。

clang:

error: call to deleted constructor of 'no_def'

Visual C :

error C2280: 'no_def::no_def(void)': attempting to reference a deleted function

您可以在https://godbolt.org/.

上亲自对此进行测试。

请注意,为了验证错误,您应该简化模板, call 摆脱未使用的可变量警告,这些警告会干扰您感兴趣的输出:

struct no_def
{
    no_def() = delete;
};
template<typename T>
void test()
{
    decltype(no_def()) a{}; // error in Clang and MSVC, no error in GCC
    a = a; // get rid of warning
}
int main()
{
    test<int>();
}

最新更新