为什么隐式类型转换在模板扣除中不起作用?

发布于 2025-02-02 13:54:15 字数 1003 浏览 2 评论 0原文

在以下代码中,我想通过将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);

In the following code, I want to call a template function by implicitly converting an int to a Scalar<int> object.

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

Why does the template argument deduction/substitution fail? And the commented-codes are also wrong.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薯片软お妹 2025-02-09 13:54:15

因为 template参数ducation 不是那么聪明:它不是(按设计),考虑用户定义的转换。和int - &gt; 标量&lt; int&gt;是用户定义的转换。

如果要使用TAD,则需要在呼叫者站点上转换参数:

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

或定义扣除指南 1 scalar 并调用f

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

另外,您可以明确实例化f(仅当scalar&lt; t&gt; :: starcar(t) is not equarlicit ):

func<int>(a, 2); 

1) 默认值扣除指南就足够了: demo

Because template argument deduction is not that smart: it does not (by design) consider user-defined conversions. And int -> Scalar<int> is a user-defined conversion.

If you want to use TAD, you need to convert your argument at the caller site:

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

or define a deduction guide1 for Scalar and call f:

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

Alternatively, you can explicitly instantiate f (only works if Scalar<T>::Scalar(T) is not explicit):

func<int>(a, 2); 

1) The default deduction guide is sufficient: demo.

紫竹語嫣☆ 2025-02-09 13:54:15
template<class Dtype>
void func(int a, Scalar<Dtype> b){ 
  std::cout << "ok" << std::endl;
}
template<class Dtype>
void func(int a, Dtype b){ 
  func(a, Scalar<Dtype>(std::move(b)));
}

模板参数扣除是模式匹配,它仅与完全匹配的类型或基本类型。它没有转换。

转换以后进行,以分辨率分辨率&amp;功能致电时间。

在这里,我们将另一个明确将其转发到您想要的过载。

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

template argument deduction is pattern matching, and it only matches the types or their base types exactly. It does no conversion.

Conversion is done later, at overload resolution & function call time.

Here, we add another overload that explicitly forwards to the one you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文