Why can't C++ deduce template type from assignment?

后端 未结 5 1070
盖世英雄少女心
盖世英雄少女心 2020-12-06 09:39

int x = fromString(\"test\") :could not deduce template argument for \'ValueType\'

int x = fromString(\"test\") : works

5条回答
  •  天命终不由人
    2020-12-06 10:30

    Besides the bad choice for an example (probably makes sense to have int x = to("1235") rather than toString), the problem is that the return type does not participate in overload resolution or type inference[1]. The reason for this is that the expression can be used in many places where the type of the return cannot be deduced:

    // assuming template  T to( std::string ):
    //
    f( to("123") );          // where there are two overloads f(int), f(double)
    int x = 1.5 * to("123"); // T == int? T == double?
    to("123");               // now what? returned object can be ignored!
    

    So the decision is that the return type will not take part in overload resolution or type deduction.

    [1] There is a single exception to this rule, which is the evaluation of a function pointer with more than one overload, where the overload must be selected by either the destination pointer or an explicit cast, but this is just the one exception and is not used in any other context:

    void f();
    void f(int);
    void g( void (*)() );
    void g( void (*)(int) );
    
    void (*p1)() = &f;      // overload selected based on destination type
    void (*p2)(int) = &f;
    g( (void (*)(int))&f ); // overload selected based on explicit cast
    

提交回复
热议问题