Why doesn't std::string provide implicit conversion to char*?

前端 未结 7 1367
滥情空心
滥情空心 2020-11-29 06:14

std::string provides const char* c_str ( ) const which:

Get C string equivalent

Generates a null-terminated sequence of cha

7条回答
  •  天涯浪人
    2020-11-29 06:24

    That's probably because this conversion would have surprising and peculiar semantics. Particularly, the fourth paragraph you quote.

    Another reason is that there is an implicit conversion const char* -> string, and this would be just the converse, which would mean strange behavior wrt overload resolution (you shouldn't make both implicit conversions A->B and B->A).

提交回复
热议问题