Why is taking the address of a temporary illegal?

前端 未结 7 1907
无人及你
无人及你 2020-12-03 08:24

I know that the code written below is illegal

void doSomething(std::string *s){}
int main()
{
     doSomething(&std::string(\"Hello World\"));
     retur         


        
7条回答
  •  再見小時候
    2020-12-03 08:47

    One reason is that your example would give the method write access to the temporary, which is pointless.

    The citation you provided isn't about this situation, it is a specific optimization that is permitted in declarators with initializers.

提交回复
热议问题