这是糟糕的c++风格还是编辑错误?

Is this a bad C++ style or an editor error?

本文关键字:编辑 风格 错误 c++      更新时间:2023-10-16

我在一个新的c++编辑器(CLion)中有这个代码:

 struct screenPoint {
        float x = 0, y = 0;
        screenPoint(float x_, float y_): x{x_}, y{y_}{}
    };
    struct position {
        screenPoint ul; 
        float width = 0, height = 0;
        position(screenPoint p, float w, float h): ul{p},width{w},height{h}{}
    };

接近末尾是初始化语句ul{p},我认为这是使用大括号初始化的有效c++方式。然而,CLion抱怨:

Incompatible types in initialiser: Types 'float' and 'screenPoint' are not compatible.

注意:没有编译错误或警告,代码按预期工作。

如果我将其更改为ul(p),错误就会消失。

现在,我知道screenPoint没有接受另一个screenPoint的构造函数,但是在这样的初始化中有必要吗?

编辑错误。具有相同类型元素的列表初始化应该调用隐式复制构造函数。

有一个关于复制聚合的核心语言缺陷(1467),但这不是一个聚合。

这肯定是CLion的问题。在跟踪器中:https://youtrack.jetbrains.com/issue/CPP-6939