initializing strings as null vs. empty string

后端 未结 6 714
独厮守ぢ
独厮守ぢ 2020-12-24 01:07

How would it matter if my C++ code (as shown below) has a string initialized as an empty string :

std::string myStr = \"\";
....some code to optionally popul         


        
6条回答
  •  天涯浪人
    2020-12-24 01:24

    Empty-ness and "NULL-ness" are two different concepts. As others mentioned the former can be achieved via std::string::empty(), the latter can be achieved with boost::optional, e.g.:

    boost::optional myStr;
    if (myStr) { // myStr != NULL
        // ...
    }
    

提交回复
热议问题