Does the template argument argument evaluate the return type?

I look at "C ++ Templates: The Complete Guide (Second Edition)", p. 10.

According to the book, subtracting a template argument does not take into account the return type.

Pattern highlighting can be seen as part of the overload resolution. A process that is not based on the choice of return types. The one exception is the return type of members of the conversion operator.

Any example will be useful when the return type is taken into account in the output.

+4
source share
2 answers
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
+5
source

I could recall another 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);      
}
0
source

Source: https://habr.com/ru/post/1686908/


All Articles