Is storage for the same content string literals guaranteed to be the same?

前端 未结 4 1763
余生分开走
余生分开走 2021-01-03 19:22

Is the code below safe? It might be tempting to write code akin to this:

#include 

const std::map m = {
    {\"text1\", 1         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-03 20:09

    No, the C++ standard makes no such guarantees.

    That said, if the code is in the same translation unit then it would be difficult to find a counter example. If main() is in a different translation then a counter example might be easier to produce.

    If the map is in a different dynamic linked library or shared object then it's almost certainly not the case.

    The volatile qualifier is a red herring.

提交回复
热议问题