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

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

    I see at least two problems with the implicit conversion:

    • Even the explicit conversion that c_str() provides is dangerous enough as is. I've seen a lot of cases where the pointer was stored to be used after the lifetime of the original string object had ended (or the object was modified thus invalidating the pointer). With the explicit call to c_str() you hopefully are aware of these issues. But with the implicit conversion it would be very easy to cause undefined behavior as in:

      const char *filename = string("/tmp/") + name;
      ofstream tmpfile(filename); // UB
    • The conversion would also happen in some cases where you wouldn't expect it and the semantics are surprising to say the least:

      string name;
      if (name) // always true
       ;
      name-2; // pointer arithmetic + UB
      These could be avoided by some means but why get into this trouble in the first place?

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-11-29 06:26

    Because C-style strings are a source of bugs and many security problems it's way better to make the conversion explicitly.

    0 讨论(0)
  • 2020-11-29 06:30

    The Josuttis book says the following:

    This is for safety reasons to prevent unintended type conversions that result in strange behavior (type char * often has strange behavior) and ambiguities (for example, in an expression that combines a string and a C-string it would be possible to convert string into char * and vice versa).

    0 讨论(0)
  • 2020-11-29 06:30

    Because implicit conversions almost never behave as you expect. They can give surprising results in overload resolution, so it's usually better to provide an explicit conversion as std::string does.

    0 讨论(0)
  • 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.

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