


Why Does Template Argument Deduction Fail with User-Defined Conversions?
Oct 27, 2024 pm 04:20 PMTemplate Argument Deduction and User-Defined Conversions
In C template programming, a common task is to pass a value of one type into a template function that expects an argument of a different type. To facilitate this, the compiler provides template argument deduction (TAD), which can automatically infer the template arguments based on the types of the actual arguments.
Limits of Template Argument Deduction
However, there are limitations to TAD. One limitation is that it does not consider user-defined conversions. This means that if you have a user-defined conversion from one type to another, TAD will not apply that conversion to infer the template arguments.
Case Study
Consider the following code snippet:
<code class="cpp">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); // Error }
In this code, we have a template function that takes two arguments: an integer a and a Scalar object of some type Dtype. In the main function, we try to call func by passing an integer a and an integer 2. However, this fails with a compilation error:
test.cpp: In function ‘int main()’: test.cpp:32:12: error: no matching function for call to ‘func(int&, int)’ func(a, 2); ^ test.cpp:32:12: note: candidate is: test.cpp:25:6: note: template<class Dtype> void func(int, Scalar<Dtype>) void func(int a, Scalar<Dtype> b){ ^ test.cpp:25:6: note: template argument deduction/substitution failed: test.cpp:32:12: note: mismatched types ‘Scalar<Dtype>’ and ‘int’ func(a, 2);</code>
Why TAD Fails
The reason for the failure is that TAD cannot apply the user-defined conversion from int to Scalar<int>. To make this work, we need to either explicitly convert the argument at the caller site or define a deduction guide for Scalar.
The above is the detailed content of Why Does Template Argument Deduction Fail with User-Defined Conversions?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

C language function format letter case conversion steps

What are the types of values returned by c language functions? What determines the return value?

What are the definitions and calling rules of c language functions and what are the

How does the C Standard Template Library (STL) work?

Where is the return value of the c language function stored in memory?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?
