Home > Backend Development > C++ > Why Does Template Type Deduction Fail with `initializer_list` in C ?

Why Does Template Type Deduction Fail with `initializer_list` in C ?

Susan Sarandon
Release: 2024-12-05 01:52:09
Original
201 people have browsed it

Why Does Template Type Deduction Fail with `initializer_list` in C  ?

initializer_list and Template Type Deduction

Template type deduction is a powerful feature of C that allows the compiler to infer the type arguments of a template from the arguments passed to the template. However, in some cases, the compiler is unable to deduce the type arguments, and the template must be explicitly instantiated with the desired type arguments.

Consider the following function:

template<typename T>
void printme(T&& t) {
  for (auto i : t)
    std::cout << i;
}
Copy after login

This function expects a single parameter with a begin()/end()-enabled type. The question is, why is the following code illegal?

printme({'a', 'b', 'c'});
Copy after login

When all of these are legitimate:

printme(std::vector<char>({'a', 'b', 'c'}));
printme(std::string("abc"));
printme(std::array<char, 3> {'a', 'b', 'c'});
Copy after login

We can even write this:

const auto il = {'a', 'b', 'c'};
printme(il);
Copy after login

Or:

printme<std::initializer_list<char>>({'a', 'b', 'c'});
Copy after login

The first line of code is illegal because the template argument T could not be inferred. If the template argument is explicitly specified, it will work, e.g.:

printme<std::vector<char>>({'a', 'b', 'c'})
Copy after login

Or:

printme<std::initializer_list<char>>({'a', 'b', 'c'})
Copy after login

The other lines of code are legal because the argument has a well-defined type, so the template argument T can be deduced just fine.

The part that may be confusing is that auto will pick the type std::initializer_list but the template argument will not. This is because § 14.8.2.5/5 of the C 11 standard explicitly states that this is a non-deduced context for a template argument:

A function parameter for which the associated argument is an initializer list (8.5.4) but the parameter does not have std::initializer_list or reference to possibly cv-qualified std::initializer_list type.
Copy after login

However, with auto, § 7.1.6.4/6 has explicit support for std::initializer_list<>:

if the initializer is a braced-init-list (8.5.4), with std::initializer_list<U>.
Copy after login

The above is the detailed content of Why Does Template Type Deduction Fail with `initializer_list` in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template