c++ - List-initialization of const references to temporary objects -


i find getting different behaviors 2 samples below g++-4.8.1, when temporaries bound instance of class:

template <class t> struct {   a(t const& a) : b(a) { }    t const& b; }; 

and

template <class t> struct b {   b(t const& a) : b{a} { }    t const& b; } 

i find, first sample, bound temporary object persists life-time of instance of a, not case instance of b. such behavior correct according c++11 standard? please point relevant parts of standard.

note: a , b, temporaries bind instantiated in expression. @ end of expression, destroyed, along temporary bind to, why life-time should same life-time of temporary.

edit: part of standard explain discrepancy between 2 initializations:

— otherwise, if t reference type, prvalue temporary of type referenced t list-initialized, , reference bound temporary. [ note: usual, binding fail , program ill-formed if reference type lvalue reference non-const type. — end note ]

such behavior not correct. see 8.5.4p3 4rd last bullet. case pre-standard drafts time, not case in c++11.

it seems confused: in no case there should temporary created. both cases should initialize reference reference. in second case, pre-standard drafts said temporary should created , bound member reference, instead of initializing reference directly.

(see number 27 in this list).


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -