gpt4 book ai didi

c++ - 在模板函数中可以使用已删除构造函数的 decltype

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:50:19 24 4
gpt4 key购买 nike

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

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 中的错误。最新的 Clang 和最新的 Visual C++ 都能正确打印诊断消息。

clang :

error: call to deleted constructor of 'no_def'

视觉 C++:

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

您可以在 https://godbolt.org/ 自行测试.


请注意,为了验证错误,您应该简化模板,调用函数,并去除干扰您感兴趣的输出的未使用变量警告:

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>();
}

关于c++ - 在模板函数中可以使用已删除构造函数的 decltype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49965849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com