Why can you assign nullptr to std::string?

前端 未结 1 881
不知归路
不知归路 2021-01-12 10:20

So today I wrote a fairly hard to find bug where I initialized a std::string to nullptr (not a pointer to std::string, but the value itself). I\'ve found apparently it\'s on

相关标签:
1条回答
  • 2021-01-12 11:08

    That's simply because there are constructors (number (5) in the link) and assignment operators (number (3) in the link) for std::string that accept a const char*, and hence the nullptr matches.

    Before C++11 (and therefore before nullptr), the same problem occurred when you tried to construct from 0 or NULL. All those cases were illegal and result in undefined behaviour, although at least one STL (RogueWave?) accepted it in the past and generated an empty string.

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