为什么“ A a {};”删除默认构造函数A :: A()时进行编译? [英] Why does 'A a{};' compile when the default constructor A::A() is deleted?

查看:115
本文介绍了为什么“ A a {};”删除默认构造函数A :: A()时进行编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是有问题的代码示例:

Here's the code example in question:

struct A {
    A() = delete;
};

int main()
{
//  A a(); // compiles, since it's a function declaration (most vexing parse)
//  A a;   // does not compile, just as expected
    A a{}; // compiles, why? The default constructor is deleted.
}

尝试此处与任何可用的编译器一起使用。我尝试了几种,但没有发现编译错误。

Try it here with any of the available compilers. I tried with several and didn't find one that gave a compilation error.

推荐答案

这是当前的语言问题,可能很快就会解决。可以找到解决必要的设计更改的提案这里。从提案的摘要中可以看出:

This is a current language issue that is very likely to be fixed soon. The proposal that tackles the necessary design change can be found here. From the abstract of the proposal:


C ++目前允许某些具有用户声明的构造函数的类型通过聚合
初始化,绕过那些构造函数。结果是令人惊讶,令人困惑的
和越野车

C++ currently allows some types with user-declared constructors to be initialized via aggregate initialization, bypassing those constructors. The result is code that is surprising, confusing, and buggy

这篇关于为什么“ A a {};”删除默认构造函数A :: A()时进行编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆