why constructors aren't explicit by default?

后端 未结 3 418
一向
一向 2021-01-01 19:56

It\'s so easy to forget to mark a constructor \"explicit\": adding/removing args, making them optional etc. the single reliable way I know is to declare every constructor as

相关标签:
3条回答
  • 2021-01-01 20:01

    Backward compatibility at the time when explicit was added. The language was evolving and changing meaning of existing constructs in an evolving language is a sure way to make your users angry. It is still the case now, backward compatibility is something high in the mind of the comittee.

    0 讨论(0)
  • 2021-01-01 20:01

    Interesting vantage point. I would tend to take the other side, and wonder why you think you need to mark all your constructors explicit until proved otherwise?

    Honestly I don't know the answer to your question, if there even is one. Backwards compatibility seems like it might be a contender for most-likely-reason, but again: why do you wish to prevent implicit conversion?

    0 讨论(0)
  • 2021-01-01 20:10

    Because when C++ was conceived it was considered that nobody would write stupid code, and everybody would enjoy the benefits that nice implicit conversions would bring in avoiding having to write casts everywhere.

    Of course, this turned out to be false, and really there are buggy constructors all over the place. Such a shame.

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