Reference initialization in C++

前端 未结 6 1634
挽巷
挽巷 2021-01-25 01:06

Greetings, everyone!

Examining my own code, I came up to this interesting line:

const CString &refStr = ( CheckCondition() ) ? _T(\"foo\") : _T(\"bar         


        
6条回答
  •  感动是毒
    2021-01-25 01:45

    You are missing something - it is completely legal code, and in fact such code is one of the commonest and best uses of the conditional operator. It's always a mistake to think that the compiler must internally do things in the same order that the code is layed out on the page - it is perfectly at liberty to evaluate the conditional operator (which is justv another expression) and then use the result to perform the initialisation.

    As for a goto, there is no way of using one in an initialisation. And if an exception is thrown, the reference is deemed never to have been created in the first place.

提交回复
热议问题