Home Backend Development C++ Why Does Template Argument Deduction Fail with User-Defined Conversions?

Why Does Template Argument Deduction Fail with User-Defined Conversions?

Oct 27, 2024 pm 04:20 PM

 Why Does Template Argument Deduction Fail with User-Defined Conversions?

Template 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&lt;typename Dtype&gt;
class Scalar{
public:
  Scalar(Dtype v) : value_(v){}
private:
  Dtype value_;
};

template&lt;typename Dtype&gt;
void func(int a, Scalar&lt;Dtype&gt; b){
  cout &lt;&lt; &quot;ok&quot; &lt;&lt;endl;
}

int main(){
  int a = 1;
  func(a, 2); // Error
}
Copy after login

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&amp;, int)’
   func(a, 2);
            ^
test.cpp:32:12: note: candidate is:
test.cpp:25:6: note: template&lt;class Dtype&gt; void func(int, Scalar&lt;Dtype&gt;)
 void func(int a, Scalar&lt;Dtype&gt; b){
      ^
test.cpp:25:6: note:   template argument deduction/substitution failed:
test.cpp:32:12: note:   mismatched types ‘Scalar&lt;Dtype&gt;’ and ‘int’
   func(a, 2);</code>
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C language function format letter case conversion steps C language function format letter case conversion steps Mar 03, 2025 pm 05:53 PM

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 types of values ​​returned by c language functions? What determines the return value? Mar 03, 2025 pm 05:52 PM

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

Gulc: C library built from scratch Gulc: C library built from scratch Mar 03, 2025 pm 05:46 PM

Gulc: C library built from scratch

What are the definitions and calling rules of c language functions and what are the What are the definitions and calling rules of c language functions and what are the Mar 03, 2025 pm 05:53 PM

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

How does the C   Standard Template Library (STL) work? How does the C Standard Template Library (STL) work? Mar 12, 2025 pm 04:50 PM

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

Where is the return value of the c language function stored in memory? Where is the return value of the c language function stored in memory? Mar 03, 2025 pm 05:51 PM

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

distinct usage and phrase sharing distinct usage and phrase sharing Mar 03, 2025 pm 05:51 PM

distinct usage and phrase sharing

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? Mar 12, 2025 pm 04:52 PM

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

See all articles