Conversion operator + conversion constructor = unintuitive behavior?

前端 未结 2 730
花落未央
花落未央 2021-01-04 12:54

I don\'t understand why the code below prints struct Value instead of int (which implies the conversion constructor is converting to Value

2条回答
  •  生来不讨喜
    2021-01-04 13:15

    In the first example Visual Studio is incorrect; the call is ambiguous. gcc in C++03 mode prints:

    source.cpp:21:34: error: call of overloaded 'Value(Convertible)' is ambiguous
    source.cpp:21:34: note: candidates are:
    source.cpp:9:5: note: Value::Value(int)
    source.cpp:6:8: note: Value::Value(const Value&)
    

    Recall that a copy constructor is implicitly defaulted. The governing paragraph is 13.3.1.3 Initialization by constructor [over.match.ctor]:

    When objects of class type are direct-initialized [...], overload resolution selects the constructor. For direct-initialization, the candidate functions are all the constructors of the class of the object being initialized.

    In the second example, deleted functions participate equally in overload resolution; they only affect compilation once overloads have been resolved, when a program that selects a deleted function is ill-formed. The motivating example in the standard is of a class that can only be constructed from floating-point types:

    struct onlydouble {
      onlydouble(std::intmax_t) = delete;
      onlydouble(double);
    };
    

提交回复
热议问题