Hariom Singh
Hariom Singh

Reputation: 3642

Does template argument deduction takes return type into account?

I am going through "C++ Templates: The Complete Guide (Second Edition)", page 10.

As per the book, template argument deduction doesn't take return type into account.

Template Deduction can be seen as part of overload resolution. A process that is not based on selection of return types. The sole exception is the return type of conversion operator members

Any example will be helpful in which return type is taken into account in deduction.

Upvotes: 4

Views: 257

Answers (2)

Cheshar
Cheshar

Reputation: 597

I could think of one more case:

template <typename A, typename B>
A foo(B)
{
    cout << "Am I being instantiated? " << __PRETTY_FUNCTION__ << endl;
    return A();
}

int main ( )
{        
    int(*fp)(int) = foo; // Instantiates "int foo(int) [A = int, B = int]"
    fp(1);      
}

Upvotes: 0

Mooing Duck
Mooing Duck

Reputation: 66991

struct A {
    int value; 

    //conversion operator
    template<class T>
    operator T() {return static_cast<T>(value);}
};

A a{4};
float f = a; //conversion from A to float

Upvotes: 5

Related Questions