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

前端 未结 7 1363
滥情空心
滥情空心 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:31

    From the C++ Programming Language 20.3.7 (emphasis mine):

    Conversion to a C-style string could have been provided by an operator const char*() rather than c_str(). This would have provided the convenience of an implicit conversion at the cost of surprises in cases in which such a conversion was unexpected.

提交回复
热议问题