Why can I prevent implicit conversions for primitives but not user-defined types?

前端 未结 3 1659
轮回少年
轮回少年 2021-01-25 18:16

The High Integrity C++ Standards suggest that rvalue arguments to functions can be deleted thus preventing implicit conversions.

http://www.codingstandard.com/rule/8-3-4

3条回答
  •  太阳男子
    2021-01-25 18:46

    The High Integrity C++ Standards suggest that rvalue arguments to functions can be deleted thus preventing implicit conversions.

    No, only a forwarding reference overload disables ICS (Implicit Conversion Sequence) for all other overloads in the overload set. Make it a forwarding reference, and see ICS disabled (Coliru Link)

    template 
    void foo(const T&&) = delete;  // introduces a qualification match
    

    The above code adds a qualification match to the overload. Thus, ICS is still in play.

    Why foo(3.3) failed is because 3.3 is an prvalue of type double which will match better with the rvalue overload than converting to int. Because qualification match is ranked better than a conversion match

提交回复
热议问题