本文介绍了非静态数据成员的类初始化中的C ++ 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的类 Foo

Suppose I have a class Foo like this:

struct Foo {
    int a;
    int b;
};

然后我定义第二个类:

struct Bar {
    Foo bar{1, 2}; // error C2661: no overloaded function takes 2 arguments
};

代码 Foo bar {1、2} 如果 bar 不是类成员,则工作正常:

The code Foo bar{1, 2} works fine if bar is not a class member:

int main() {
    Foo bar{1, 2}; // OK
}

类<$ c $中的代码是否有错误c> Bar ?

推荐答案

您的代码没有错。这是一个。

There's nothing wrong with your code. It's a compiler bug.

clang ++和g ++都正确处理了这些合格的列表初始化程序。请参见进行g ++演示正确的行为。

Both clang++ and g++ process these qualified list initializers correctly. See http://coliru.stacked-crooked.com/a/5d45e3645eec0476 for g++ demonstrating correct behavior.

这篇关于非静态数据成员的类初始化中的C ++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 21:05
查看更多