c ++将初始化列表传递给函数作为参数将构造一个对象?

时间:2022-05-10 11:00:49

Here is my code:

这是我的代码:

struct MyTest{
    int a;
    char b;
};
void testFunc(MyTest mt){
    cout << mt.a << mt.b << endl;
}
void main(){
    testFunc({ 1, 'c' });
}

It seems that mt will get constructed from the initializer_list and testFunc will output the right result. But how can it be since I didn't implement a constructor accepting an initializer_list at all.

似乎mt将从initializer_list构造,testFunc将输出正确的结果。但是,由于我没有实现一个接受initializer_list的构造函数,所以它怎么可能呢。

4 个解决方案

#1


7  

This class is an aggregate - a simple structure containing public data members and no user-defined constructors and the like.

这个类是一个聚合 - 一个包含公共数据成员的简单结构,没有用户定义的构造函数等。

List-initialisation is supported for aggregate types; the elements of the initialiser list are used to initialise each data member in turn. This has been supported for variable initialisation in both C and C++ for a very long time:

聚合类型支持列表初始化;初始化列表的元素用于依次初始化每个数据成员。在很长一段时间内,C和C ++中的变量初始化都支持这一点:

MyTest x = {1, 'c'};

and C++11 extended this to other uses of list-initialisation, including (in this case) initialising a temporary function argument.

和C ++ 11将其扩展到列表初始化的其他用途,包括(在这种情况下)初始化临时函数参数。

#2


3  

list-initialization is occurring. Since (as in C) you can initialize the type with

列表初始化正在发生。由于(如在C中)您可以使用初始化类型

MyTest mt = {1, 'c'};

the same initialization takes place at the point of the call

同样的初始化发生在呼叫点

(N3690) 8.5/17 [...]

(N3690)8.5 / 17 [...]

-- If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized

- 如果初始化程序是(非括号的)braced-init-list,则对象或引用是列表初始化的

8.5.4/3 gives the example:

8.5.4 / 3举例说明:

struct S2 { // OK
  int m1; 
  double m2, m3; 
}; 
S2 s21 = { 1, 2, 3.0 };  // OK

Calling the function with an initializer list behaves the same way. Further:

使用初始化列表调用函数的行为方式相同。进一步:

(8.5.1/1) An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3). When an aggregate is initialized by an initializer list, as specified in 8.5.4, the elements of the initializer list are taken as initializers for the members of the aggregate, in increasing subscript or member order.

(8.5.1 / 1)聚合是一个数组或类(第9条),没有用户提供的构造函数(12.1),没有私有或受保护的非静态数据成员(第11条),没有基类(第10条) ,没有虚函数(10.3)。当初始化程序列表初始化聚合时,如8.5.4中所述,初始化程序列表的元素将作为聚合成员的初始化程序,增加下标或成员顺序。

#3


0  

In C++11 we don't have to define a constructor for a POD. We can use the new list-initialization syntax:

在C ++ 11中,我们不必为POD定义构造函数。我们可以使用新的列表初始化语法:

MyTest mt{1, 'c'};
MyTest mt2 = {1, 'c'};

this is what's being called in your code.

这就是您的代码中调用的内容。

#4


-4  

Well, first of you main function is wrong:

那么,首先你的主要功能是错误的:

int main() {
/* ... code ... here */
return 0;
}

Second, you need a object to initialize your struct:

其次,您需要一个对象来初始化您的结构:

#include <iostream>

struct Foo {
    char _a;
    int _b;
};

void print_them (const Foo& foo) {
    std::cout << foo._a << ", " << foo._b << std::endl;
}

int main () {
    /* create object and initialize it */
    Foo f = {
        'a', 7
    };
    /* call function */
    print_them (f);
    return 0;
}

#1


7  

This class is an aggregate - a simple structure containing public data members and no user-defined constructors and the like.

这个类是一个聚合 - 一个包含公共数据成员的简单结构,没有用户定义的构造函数等。

List-initialisation is supported for aggregate types; the elements of the initialiser list are used to initialise each data member in turn. This has been supported for variable initialisation in both C and C++ for a very long time:

聚合类型支持列表初始化;初始化列表的元素用于依次初始化每个数据成员。在很长一段时间内,C和C ++中的变量初始化都支持这一点:

MyTest x = {1, 'c'};

and C++11 extended this to other uses of list-initialisation, including (in this case) initialising a temporary function argument.

和C ++ 11将其扩展到列表初始化的其他用途,包括(在这种情况下)初始化临时函数参数。

#2


3  

list-initialization is occurring. Since (as in C) you can initialize the type with

列表初始化正在发生。由于(如在C中)您可以使用初始化类型

MyTest mt = {1, 'c'};

the same initialization takes place at the point of the call

同样的初始化发生在呼叫点

(N3690) 8.5/17 [...]

(N3690)8.5 / 17 [...]

-- If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized

- 如果初始化程序是(非括号的)braced-init-list,则对象或引用是列表初始化的

8.5.4/3 gives the example:

8.5.4 / 3举例说明:

struct S2 { // OK
  int m1; 
  double m2, m3; 
}; 
S2 s21 = { 1, 2, 3.0 };  // OK

Calling the function with an initializer list behaves the same way. Further:

使用初始化列表调用函数的行为方式相同。进一步:

(8.5.1/1) An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3). When an aggregate is initialized by an initializer list, as specified in 8.5.4, the elements of the initializer list are taken as initializers for the members of the aggregate, in increasing subscript or member order.

(8.5.1 / 1)聚合是一个数组或类(第9条),没有用户提供的构造函数(12.1),没有私有或受保护的非静态数据成员(第11条),没有基类(第10条) ,没有虚函数(10.3)。当初始化程序列表初始化聚合时,如8.5.4中所述,初始化程序列表的元素将作为聚合成员的初始化程序,增加下标或成员顺序。

#3


0  

In C++11 we don't have to define a constructor for a POD. We can use the new list-initialization syntax:

在C ++ 11中,我们不必为POD定义构造函数。我们可以使用新的列表初始化语法:

MyTest mt{1, 'c'};
MyTest mt2 = {1, 'c'};

this is what's being called in your code.

这就是您的代码中调用的内容。

#4


-4  

Well, first of you main function is wrong:

那么,首先你的主要功能是错误的:

int main() {
/* ... code ... here */
return 0;
}

Second, you need a object to initialize your struct:

其次,您需要一个对象来初始化您的结构:

#include <iostream>

struct Foo {
    char _a;
    int _b;
};

void print_them (const Foo& foo) {
    std::cout << foo._a << ", " << foo._b << std::endl;
}

int main () {
    /* create object and initialize it */
    Foo f = {
        'a', 7
    };
    /* call function */
    print_them (f);
    return 0;
}