0

Why didn't C++0x deprecate implicit conversions for user defined types a.k.a. objects? Is there any project which actually uses this (mis)feature? Whenever I see a single argument constructor in a code I get to review or modify I treat it as bug and make it explicit. So far it worked well and nobody complained.

Thank you.

EDIT: Let me quote Alex Stepanov, the creator of STL:

Open your C++ book and read about the explicit keyword! Also petition your neighborhood C++ standard committee member to finally abolish implicit conversions. There is a common misconception, often propagated by people who should know better, that STL depends on implicit conversions. Not so!

Reference: A. Stepanov. C++ notes

EDIT AGAIN: No, no debate plz. I am just curious whether anyone uses implicit conversions in their work. I never seen any project which would allow implicit conversion for objects. I thought hard and couldn't come with any hypothetical scenario where implicit conversion wouldn't become a minefield. I mean C++ single argument conversions, not float->double or similar conversions inherited from C.

9
  • 8
    You never pass a string literal to a function with an argument of type std::string?
    – Ben Voigt
    May 1, 2011 at 23:39
  • 1
    Could std::string be made an exception? It belongs to C++ standard and compiler could treat it differently. Do you know anyone who uses implicit conversion for user defined types? I see people are using explicit keyword everywhere, even in multiple argument constructors. Many people make every constructor explicit, just in case, because it doesn't hurt. But implicit conversion can hurt badly.
    – pic11
    May 2, 2011 at 0:00
  • 2
    Much of the allure of C++ is that libraries are pretty much equal, whether it's the standard library or user-defined libraries. I don't want special treatment of std::string, I want to be able to download someone's utf8string class and have it function just as smoothly as std::string.
    – Ben Voigt
    May 2, 2011 at 0:00
  • Why would someone downvote or close this question? IMO it's a valid one, even though the answer is simple. (Implicit conversions is something we inherited from C, which introduced it 40 years ago, and which was expanded int C++' type system almost 30 years ago. While those who designed C and C++ that way would most likely not do this again, we're now stuck with billions of lines of working code relying on this, which we can't break.)
    – sbi
    May 2, 2011 at 6:55
  • @sbi: If the language were redesigned from scratch, I don't think implicit conversions would be gone. There might not be implicit implicit conversions, instead requiring an implicit keyword as Ronald's answer mentions. But there is no good reason that widening conversions should not remain implicit.
    – Ben Voigt
    May 2, 2011 at 17:46

2 Answers 2

7

The obvious answer is that code written and working in C++03 is supposed to continue working with C++0x compilers.

3
2

For one thing, it would be a hugely breaking change to remove implicit conversion from the language - even if it were made optional and off-by-default with an implicit keyword.

I've done a search of comp.std.c++ and it doesn't seem to have been discussed at all in that group - though there have been some questions on the subject, no-one seems to have suggested going so far as removing it. I would certainly not go so far either: it's a feature I happily use on occasion and I do not subscribe to making all possibly-converting constructors explicit either - unless it causes real bugs.

3
  • Instead of introducing another keyword, you could use !explicit ;) May 2, 2011 at 8:26
  • @Fred wouldn't ~explicit be more their style?
    – KitsuneYMG
    May 12, 2011 at 12:38
  • @Kit: Good suggestion, I like it :) May 12, 2011 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.