gpt4 book ai didi

c++ - 引用初始化表格

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:28:33 28 4
gpt4 key购买 nike

所以我正在测试一些引用初始化形式,描述here .我想知道什么时候:

T & ref = { arg1, arg2, ... };

T && ref = { arg1, arg2, ... };

表格将永远被使用,并且究竟是为了什么。我想这是为了用“initializer_list”初始化临时数组和构造函数,如下所示:

int main()
{


struct _ab
{
_ab() {cout << "_ab()" << endl;}

_ab(initializer_list<int> iArr) : a(*iArr.begin()), b(*iArr.end()) {cout << "_ab()" << endl;}

~_ab() {cout << "~_ab()" << endl;}
int a, b;
};
const _ab & i = {1, 2};

cout << i.a << endl;

return 0;
}

在这种情况下,我尝试使用默认构造函数使用临时“_ab”对象初始化 const 引用,如下所示:

int main()
{


struct _ab
{
_ab() {cout << "_ab()" << endl;}

_ab(initializer_list<int> iArr) : a(*iArr.begin()), b(*iArr.end()) {cout << "_ab()" << endl;}

~_ab() {cout << "~_ab()" << endl;}
int a, b;
};
const _ab & i(); // error 1

cout << i.a << endl; // error 2

return 0;
}

但是这个例子没有编译,有 2 个错误:

error 1: 'const main()::_ab& i()', declared using local type 'const main()::_ab', is used but never defined [-fpermissive]|

error 2: request for member 'a' in 'i', which is of non-class type 'const main()::_ab&()'|

你能告诉我上面两个结构的含义和用途吗?

编辑:我理解第二个例子的问题。它正在声明一个函数而不是变量。但是仍然有人可以解释为什么引用有可能使用初始化列表进行初始化以及它的用途是什么?

最佳答案

const _ab & i();

标准在 [dcl.init]/8 的注释中对此进行了解释:

[ Note: Since () is not permitted by the syntax for initializer,

X a();

is not the declaration of an object of class X, but the declaration of a function taking no argument and returning an X.

这适用于任何类型,被称为最令人烦恼的解析。当然 i.a 是病式的。


But still can somebody explain me why references have the possibility to be initialized with initialization list and for what is it used?

[dcl.init]/3:

List-initialization of an object or reference of type T is defined as follows:

  • [..]

  • Otherwise, if the initializer list has a single element of type E and either T is not a reference type or its referenced type is reference-related to E, the object or reference is initialized from that element; if a narrowing conversion (see below) is required to convert the element to T, the program is ill-formed.

  • Otherwise, if T is a reference type, a prvalue temporary of the type referenced by T is copy-list-initialized or direct-list-initialized, depending on the kind of initialization for the reference, and the reference is bound to that temporary.
    [ Note: As usual, the binding will fail and the program is ill-formed if the reference type is an lvalue reference to a non-const type. — end note ]

最后的注释是指

int& i = {1};

格式错误,因为我们必须将 1 绑定(bind)到非常量左值引用,这是不可能的。也不会

std::string& s = {20, '5'};

是有效的,因为我们必须用临时值初始化一个非常量左值引用。
该标准随后给出了示例:

struct S {
S(std::initializer_list<double>); // #1
S(const std::string&); // #2
// ...
};

const S& r1 = { 1, 2, 3.0 }; // OK: invoke #1
const S& r2 { "Spinach" }; // OK: invoke #2
S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
const int& i1 = { 1 }; // OK
const int& i2 = { 1.1 }; // error: narrowing
const int (&iar)[2] = { 1, 2 }; // OK: iar is bound to temporary array

这对于函数调用也特别有用。

void f(std::vector<int> const&);

f( {first, last} );

关于c++ - 引用初始化表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27090093/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com