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

前端 未结 7 2055
闹比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:16

    You can circumvent this problem by using braced initialization. For example:

    struct A {
      A(int _a) : a(_a) {}
      int a;
    };
    
    A a{5}; // ok
    A b{1.123}; // compile error
    

    Proof

提交回复
热议问题