Avoiding implicit conversion in constructor. The 'explicit' keyword doesn't help here

前端 未结 7 2029
闹比i
闹比i 2020-12-29 18:07

I am able to avoid the implicit conversion of a constructor using the explicit keyword. So now, conversions like A a1 = 10; can be avoided.

相关标签:
7条回答
  • 2020-12-29 18:36

    The way to achieve this is to provide another constructor that would be a better match, and then delete it so you'll get an error. For your class, adding

    template <typename T>
    A(T) = delete;
    

    Will stop the class from being constructed from anything that isn't an int

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