Link Initialization Forms

So, I tested some initialization reference forms described here . I wonder when:

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

and

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

forms will ever be used and for what purpose. I assume that it initializes temporary arrays and constructors using "initializer_list", for example:

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;
}

In this context, I tried to initialize a const reference with a temporary "_ab" object using a default constructor similar to this:

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;
    }

But this example did not compile with two errors:

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

2: 'a' 'i', non-class 'const main():: _ ab &()' |

, , .

EDIT: . . - , , ?

+4
3
const _ab & i();

[dcl.init]/8:

[: () ,

X a();

X, , X.

. , i.a, , .


- , , ?

[dcl.init]/3:

- T :

  • [..]

  • , E, T , , E, ; (. ) T, .

  • , T , praleue , T, direct-list-initialized, .
    [. , , , - lvalue . - ]

,

int& i = {1};

, 1 lvalue, .

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} );
+4

const _ab & i();

( ) , const _ab struct, - . , - (i), .

, {...} ++ 11,

const _ab & i {}; // probably will work fine

- :

const _ab & i;  // error

:

const _ab & i = {};

EDIT:

"" ( ), const. , , . , ( - ) - , "" . explicit, , , , , :

const _ab & i = _ab{/*...*/};

:

const _ab anonymousObject = _ab{/*...*/};
const _ab & i = anonymousObject;
+3

, , , rvalue const-reference, .

std::string str() { return "blah"; }

const std::string& s = str();

, , , , .

:

const std::string& s = std::string();

( ).

const _ab& i = {1, 2} , _ab, init-i, :

const _ab& i = _ab{1, 2};

( , , initializer_list _ab explicit, ... - explicit , , ).

, , const _ab{1, 2}; . , -, , . , std::pair<int, const char*>, func({1, "two"}), , ++ 03: func(std::make_pair(1, "two")). , , , , const _ab& = {1, 2}, .

0

All Articles