【问题标题】:Why does the implicit type conversion not work in template deduction?为什么隐式类型转换在模板推导中不起作用?
【发布时间】:2018-11-09 14:33:39
【问题描述】:

在以下代码中,我想通过将int 隐式转换为Scalar<int> 对象来调用模板函数。

#include<iostream>
using namespace std;

template<typename Dtype>
class Scalar{
public:
  Scalar(Dtype v) : value_(v){}
private:
  Dtype value_;
};

template<typename Dtype>
void func(int a, Scalar<Dtype> b){ 
  cout << "ok" <<endl;
}

int main(){
  int a = 1;
  func(a, 2); 
  //int b = 2;
  //func(a, b);
  return 0;
}

为什么模板参数推导/替换失败?并且注释代码也是错误的。

test.cpp: In function ‘int main()’:
test.cpp:19:12: error: no matching function for call to ‘func(int&, int)’
   func(a, 2);
            ^
test.cpp:19:12: note: candidate is:
test.cpp:13:6: note: template<class Dtype> void func(int, Scalar<Dtype>)
 void func(int a, Scalar<Dtype> b){
      ^
test.cpp:13:6: note:   template argument deduction/substitution failed:
test.cpp:19:12: note:   mismatched types ‘Scalar<Dtype>’ and ‘int’
   func(a, 2);

【问题讨论】:

标签: c++ c++11 templates


【解决方案1】:

因为template argument deduction 并不那么聪明:它(按设计)不考虑用户定义的转换。而int -> Scalar&lt;int&gt; 是用户定义的转换。

如果你想使用TAD,你需要在调用者站点转换你的参数:

func(a, Scalar<int>{2}); 

或为Scalar定义一个扣除指南1并调用f

func(a, Scalar{2}); // C++17 only

或者,您可以显式实例化f

func<int>(a, 2); 

1) 默认扣款指南就足够了:demo.

【讨论】:

  • 我已经测试了您的所有三个解决方案,它们将按原样工作;但是,如果 OP 将 Scalar() 声明为 explicit,则第三个将无法编译。不确定这是否值得一提,但可能对 OP 来说很重要。
【解决方案2】:
template<typename Dtype>
void func(int a, Scalar<Dtype> b){ 
  cout << "ok" <<endl;
}
template<typename Dtype>
void func(int a, Dtype b){ 
  func(a, Scalar<Dtype>(std::move(b)));
}

模板参数推导是模式匹配,它只精确匹配类型或其基类型。它没有转换。

转换稍后完成,在重载决议和函数调用时完成。

在这里,我们添加了另一个重载,显式转发给您想要的。

【讨论】:

  • 您可能希望b 成为Dtype&amp;&amp; 并使用std::forward 而不是std::move 进行完美转发。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
  • 2013-08-11
  • 2021-12-06
  • 1970-01-01
相关资源
最近更新 更多