【C/C++】非静态成员初始化器,类内为什么不允许使用圆括号的方式初始化?

C++ 期末考了这道题:
在这里插入图片描述
我惊讶地发现,int a(0), b(0); 是错误的。
我们知道,自 C++11 起已经支持非静态成员变量的类内初始化了。
而对基本数据类型的初始化形式 int a(0); 也是没有争议的,那么为什么类内初始化是不被允许的?
我找到了这篇文章:Non-static data member initializers(非静态数据成员初始化器)
大概说的是一些语义不清晰的问题。
有时候我们无法避免下列情况。

Problem 1:
Unfortunately, this makes initializers of the “( expression-list )” form ambiguous at the time that the declaration is being parsed:
    struct S {
    
    
        int i(x); // data member with initializer
        // ...
        static int x;
    };

    struct T {
    
    
        int i(x); // member function declaration
        // ...
        typedef int x;
    };

猜你喜欢

转载自blog.csdn.net/qq_16181837/article/details/107377500