是否会考虑返回类型

Does template argument deduction takes return type into account?

本文关键字:返回类型 是否      更新时间:2023-10-16

我正在通过" C 模板:完整指南(第二版)",第10页。

根据本书,模板参数扣除未考虑返回类型。

模板扣除可以看作是超负荷分辨率的一部分。不基于返回类型的选择的过程。唯一的例外是转换操作员成员的返回类型

任何示例都会有所帮助,在扣除中考虑返回类型。

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

我可以想到另一种情况:

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);      
}