是否会考虑返回类型
Does template argument deduction takes return type into account?
我正在通过" 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);
}
相关文章:
- 函数作为模板参数,是否对返回类型强制约束
- 检查函数返回类型是否与STL容器类型值相同
- 当返回类型声明为 ListNode 时,我们是否可以返回 false<T>*
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 是否创建具有不同返回类型的lambda
- 如果函数是在类的主体中定义的,我是否需要在成员函数的返回类型中指定 typename?
- decltype(auto) 是否使尾随返回类型过时?
- clang 拒绝具有尾随 decltype 返回类型的模板调用是否正确,具体取决于其重载之一?
- C++中的函数是否有任何默认返回类型
- 是否可以将一种函数类型转换为另一种采用相同参数但返回类型不同的函数类型
- 是否可以在尾随返回类型语法中直接使用参数值(不是其类型,而是值本身)
- 是否可以为类设置自定义返回类型
- 是否可以将“自动”关键字用作函数指针声明中使用初始化的返回类型
- 在C++中使用指针或引用作为函数的返回类型是否有经验法则
- 表达式的返回类型是否始终与操作数类型匹配
- 函数返回类型是否会影响重载的选择
- 更改函数指针的签名并调用它以忽略返回类型是否安全?
- 如果我想使类不可复制,"operator="返回类型是否重要?
- C++流插入运算符的返回类型是否需要是std::ostream