Binding temporary to const reference in c'tor initializer list

前端 未结 1 683
天涯浪人
天涯浪人 2021-02-20 01:16

Section 12.2.5 in C++03 says \"A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits\"
S

相关标签:
1条回答
  • 2021-02-20 01:31

    I have tried this with MS VS 2010, and it gives me the output also gives warning during compile:

    warning C4413: 'bar::ref' : reference member is initialized to a temporary that doesn't persist after the constructor exits

    foo c'tor
    bar c'tor
    foo d'tor
    Press any key to continue . . .
    

    It seems that MS VS 2010 implements specification correctly. I agree that it is a bug for g++.

    EDIT: ref should be initialized in constructor`s initialize list.

    0 讨论(0)
提交回复
热议问题