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

前端 未结 7 2034
闹比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条回答
  •  Happy的楠姐
    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 
    A(T) = delete;
    

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

提交回复
热议问题