将任何类型的表达式放在 C++ 的初始化列表中在语法上是否正确?

Is it syntactically correct to put any kind of expressions in c++'s initialization list?

我从教科书中看到了这个 class 定义:

class Smiley : public Circle { // use the circle as the base for a face public:

Smiley(Point p, int r) : Circle{p,r}, mouth{nullptr} { }

我对构造函数的初始化列表有疑问。 Circle{p,r} 部分应该是关于构建一个基础对象,它看起来不像是你放在初始化列表中的东西。这是否意味着您可以在初始化列表中放入任何类型的表达式?

The Circle{p,r} part should be about constructing a base object

正确。

which does not look like something you put in an initialization list

不管您怎么看,成员初始化器列表都是初始化基础子对象的地方。尽管其名称中包含 member。从技术上讲,子对象初始化器列表将是更准确的名称。

Does it mean that you can put any kind of expressions in the initialization list?

没有;目前还不清楚你是如何得出这样的结论的。成员初始化列表语法如下:

ctor-initializer:
    : mem-initializer-list

mem-initializer-list:
    mem-initializer ... opt
    mem-initializer-list , mem-initializer ... opt

mem-initializer:
    mem-initializer-id ( expression-list opt )
    mem-initializer-id braced-init-list

mem-initializer-id:
    class-or-decltype
    identifier

如果mem-initializer-idclass-or-decltypemem-initializer是一个基地,identifier是一个成员。

在expression-list中,你也不能放任意种类的表达式。语法是:

expression-list:
    initializer-list

initializer-list:
    initializer-clause ... opt
    initializer-list , initializer-clause ... opt

initializer-clause:
    assignment-expression
    braced-init-list

braced-init-list:
    { initializer-list , opt }
    { designated-initializer-list , opt }
    { }