命名空间
变体
操作

模板参数推导

来自 cppreference.cn
< cpp‎ | language
 
 
C++ 语言
 
 
 
 

为了实例化一个函数模板,每个模板实参都必须是已知的,但并非每个模板实参都必须被指定。如果可能,编译器将从函数实参中推导出缺失的模板实参。这发生在尝试函数调用、获取函数模板的地址以及在一些其他上下文

template<typename To, typename From>
To convert(From f);
 
void g(double d)
{
    int i = convert<int>(d);    // calls convert<int, double>(double)
    char c = convert<char>(d);  // calls convert<char, double>(double)
    int(*ptr)(float) = convert; // instantiates convert<int, float>(float)
                                // and stores its address in ptr
}

这种机制使得使用模板运算符成为可能,因为除了将其重写为函数调用表达式之外,没有语法可以为运算符指定模板实参

#include <iostream>
 
int main()
{
    std::cout << "Hello, world" << std::endl;
    // operator<< is looked up via ADL as std::operator<<,
    // then deduced to operator<<<char, std::char_traits<char>> both times
    // std::endl is deduced to &std::endl<char, std::char_traits<char>>
}

模板实参推导发生在函数模板名称查找(可能涉及实参依赖查找)之后,以及模板实参替换(可能涉及SFINAE)和重载解析之前。

当类模板的名称用作正在构造的对象的类型时,也会执行模板实参推导

std::pair p(2, 4.5);
std::tuple t(4, 3, 2.5);
std::copy_n(vi1, 3, std::back_insert_iterator(vi2));
std::for_each(vi.begin(), vi.end(), Foo([&](int i) {...}));
auto lck = std::lock_guard(foo.mtx);
std::lock_guard lck2(foo.mtx, ul);

类模板的模板实参推导发生在声明和显式转型表达式中;有关详细信息,请参阅类模板实参推导

(自 C++17 起)

目录

[编辑] 从函数调用中推导

模板实参推导尝试确定模板实参(类型模板形参 Ti 的类型,模板模板形参 TTi 的模板,以及非类型模板形参 Ii 的值),这些实参可以代入每个形参 P 中以产生推导的类型 A,该类型与实参 A 的类型相同,在下面列出的调整之后。

如果有多个形参,则每个 P/A 对被单独推导,然后将推导出的模板实参组合在一起。如果对于任何 P/A 对,推导失败或不明确,或者不同的对产生不同的推导模板实参,或者如果任何模板实参既未被推导也未被显式指定,则编译失败。

如果从 P 中移除引用和 cv 限定符得到 std::initializer_list<P'> 并且 A 是一个花括号初始化列表,则对初始化列表的每个元素执行推导,将 P' 作为形参,列表元素 A' 作为实参

template<class T>
void f(std::initializer_list<T>);
 
f({1, 2, 3});  // P = std::initializer_list<T>, A = {1, 2, 3}
               // P'1 = T, A'1 = 1: deduced T = int
               // P'2 = T, A'2 = 2: deduced T = int
               // P'3 = T, A'3 = 3: deduced T = int
               // OK: deduced T = int
 
f({1, "abc"}); // P = std::initializer_list<T>, A = {1, "abc"}
               // P'1 = T, A'1 = 1: deduced T = int
               // P'2 = T, A'2 = "abc": deduced T = const char*
               // error: deduction fails, T is ambiguous

如果从 P 中移除引用和 cv 限定符得到 P'[N],并且 A 是一个非空的花括号初始化列表,则执行与上述相同的推导,除非 N 是一个非类型模板形参,它从初始化列表的长度中推导出来

template<class T, int N>
void h(T const(&)[N]);
h({1, 2, 3}); // deduced T = int, deduced N = 3
 
template<class T>
void j(T const(&)[3]);
j({42}); // deduced T = int, array bound is not a parameter, not considered
 
struct Aggr
{
    int i;
    int j;
};
 
template<int N>
void k(Aggr const(&)[N]);
k({1, 2, 3});       // error: deduction fails, no conversion from int to Aggr
k({{1}, {2}, {3}}); // OK: deduced N = 3
 
template<int M, int N>
void m(int const(&)[M][N]);
m({{1, 2}, {3, 4}}); // deduced M = 2, deduced N = 2
 
template<class T, int N>
void n(T const(&)[N], T);
n({{1}, {2}, {3}}, Aggr()); // deduced T = Aggr, deduced N = 3

如果形参包作为最后一个 P 出现,则将类型 P 与调用的每个剩余实参的类型 A 匹配。每个匹配都为包展开中的下一个位置推导出模板实参

template<class... Types>
void f(Types&...);
 
void h(int x, float& y)
{
    const int z = x;
    f(x, y, z); // P = Types&..., A1 = x: deduced first member of Types... = int
                // P = Types&..., A2 = y: deduced second member of Types... = float
                // P = Types&..., A3 = z: deduced third member of Types... = const int
                // calls f<int, float, const int>
}


(自 C++11 起)

如果 P 是函数类型、指向函数类型的指针或指向成员函数类型的指针,并且如果 A 是一个重载函数集合,不包含函数模板,则尝试对每个重载进行模板实参推导。如果只有一个成功,则使用该成功的推导。如果零个或多个成功,则模板形参是非推导语境(见下文)

template<class T>
int f(T(*p)(T));
 
int g(int);
int g(char);
 
f(g); // P = T(*)(T), A = overload set
      // P = T(*)(T), A1 = int(int): deduced T = int
      // P = T(*)(T), A2 = int(char): fails to deduce T
      // only one overload works, deduction succeeds

在推导开始之前,对 PA 进行以下调整

1) 如果 P 不是引用类型,
a) 如果 A 是数组类型,则 A 被替换为从数组到指针转换获得的指针类型;
b) 否则,如果 A 是函数类型,则 A 被替换为从函数到指针转换获得的指针类型;
c) 否则,如果 A 是 cv 限定类型,则顶层 cv 限定符在推导时被忽略
template<class T>
void f(T);
 
int a[3];
f(a); // P = T, A = int[3], adjusted to int*: deduced T = int*
 
void b(int);
f(b); // P = T, A = void(int), adjusted to void(*)(int): deduced T = void(*)(int)
 
const int c = 13;
f(c); // P = T, A = const int, adjusted to int: deduced T = int
2) 如果 P 是 cv 限定类型,则顶层 cv 限定符在推导时被忽略。
3) 如果 P 是引用类型,则引用的类型用于推导。
4) 如果 P 是对 cv 非限定模板形参的右值引用(所谓的转发引用),并且相应的函数调用实参是左值,则类型为对 A 的左值引用将代替 A 用于推导(注意:这是 std::forward操作的基础。注意:在类模板实参推导中,类模板的模板形参永远不是转发引用(自 C++17 起)
template<class T>
int f(T&&);       // P is an rvalue reference to cv-unqualified T (forwarding reference)
 
template<class T>
int g(const T&&); // P is an rvalue reference to cv-qualified T (not special)
 
int main()
{
    int i;
    int n1 = f(i); // argument is lvalue: calls f<int&>(int&) (special case)
    int n2 = f(0); // argument is not lvalue: calls f<int>(int&&)
 
//  int n3 = g(i); // error: deduces to g<int>(const int&&), which
                   // cannot bind an rvalue reference to an lvalue
}

在这些转换之后,推导过程如下所述(参见从类型中推导部分),并尝试找到这样的模板实参,使推导出的 A(即,在上面列出的调整和推导出的模板形参的替换之后的 P)与转换后的 A 相同,即上面列出的调整之后的 A

如果从 PA 的通常推导失败,则额外考虑以下替代方案

1) 如果 P 是引用类型,则推导出的 A(即,引用所指的类型)可以比转换后的 A 具有更多的 cv 限定符
template<typename T>
void f(const T& t);
 
bool a = false;
f(a); // P = const T&, adjusted to const T, A = bool:
      // deduced T = bool, deduced A = const bool
      // deduced A is more cv-qualified than A
2) 转换后的 A 可以是另一个指针或指向成员类型的指针,可以通过限定转换或函数指针转换(自 C++17 起)转换为推导出的 A
template<typename T>
void f(const T*);
 
int* p;
f(p); // P = const T*, A = int*:
      // deduced T = int, deduced A = const int*
      // qualification conversion applies (from int* to const int*)
3) 如果 P 是一个类,并且 P 具有简单模板 ID 的形式,则转换后的 A 可以是推导出的 A 的派生类。同样,如果 P 是指向形式为简单模板 ID的类的指针,则转换后的 A 可以是指向推导出的 A 所指向的派生类的指针
template<class T>
struct B {};
 
template<class T>
struct D : public B<T> {};
 
template<class T>
void f(B<T>&) {}
 
void f()
{
    D<int> d;
    f(d); // P = B<T>&, adjusted to P = B<T> (a simple-template-id), A = D<int>:
          // deduced T = int, deduced A = B<int>
          // A is derived from deduced A
}

[编辑] 非推导语境

在以下情况下,用于组成 P 的类型、模板和非类型值不参与模板实参推导,而是使用在其他地方推导或显式指定的模板实参。如果模板形参仅在非推导语境中使用,并且未显式指定,则模板实参推导失败。

1) 嵌套名称说明符(作用域解析运算符 :: 左侧的所有内容)的类型,该类型是使用限定 ID 指定的
// the identity template, often used to exclude specific arguments from deduction
// (available as std::type_identity as of C++20)
template<typename T>
struct identity { typedef T type; };
 
template<typename T>
void bad(std::vector<T> x, T value = 1);
 
template<typename T>
void good(std::vector<T> x, typename identity<T>::type value = 1);
 
std::vector<std::complex<double>> x;
 
bad(x, 1.2);  // P1 = std::vector<T>, A1 = std::vector<std::complex<double>>
              // P1/A1: deduced T = std::complex<double>
              // P2 = T, A2 = double
              // P2/A2: deduced T = double
              // error: deduction fails, T is ambiguous
 
good(x, 1.2); // P1 = std::vector<T>, A1 = std::vector<std::complex<double>>
              // P1/A1: deduced T = std::complex<double>
              // P2 = identity<T>::type, A2 = double
              // P2/A2: uses T deduced by P1/A1 because T is to the left of :: in P2
              // OK: T = std::complex<double>
2) 包索引说明符包索引表达式
template<typename... Ts>
void f(Ts...[0], std::tuple<Ts...>);
 
f(3, std::tuple(5, 'A'));
// P2 = std::tuple<Ts...>, A2 = std::tuple<int, char>
// P2/A2: deduced first member of Ts... = int
// P2/A2: deduced second member of Ts... = char
// P1 = Ts...[0], A1 = int: Ts...[0] is in non-deduced context
(自 C++26 起)
3) decltype-说明符的表达式
template<typename T>
void f(decltype(*std::declval<T>()) arg);
 
int n;
f<int*>(n); // P = decltype(*declval<T>()), A = int: T is in non-deduced context
(自 C++11 起)
4) 非类型模板实参或数组边界,其中子表达式引用模板形参
template<std::size_t N>
void f(std::array<int, 2 * N> a);
 
std::array<int, 10> a;
f(a); // P = std::array<int, 2 * N>, A = std::array<int, 10>:
      // 2 * N is non-deduced context, N cannot be deduced
      // note: f(std::array<int, N> a) would be able to deduce N
5) 在函数形参的形参类型中使用的模板形参,该函数形参具有默认实参,该默认实参正在用于为其执行实参推导的调用中
template<typename T, typename F>
void f(const std::vector<T>& v, const F& comp = std::less<T>());
 
std::vector<std::string> v(3);
f(v); // P1 = const std::vector<T>&, A1 = std::vector<std::string> lvalue
      // P1/A1 deduced T = std::string
      // P2 = const F&, A2 = std::less<std::string> rvalue
      // P2 is non-deduced context for F (template parameter) used in the
      // parameter type (const F&) of the function parameter comp,
      // that has a default argument that is being used in the call f(v)
6) 形参 P,其 A 是一个函数或一组重载,使得多个函数与 P 匹配,或者没有函数与 P 匹配,或者重载集合包含一个或多个函数模板
template<typename T>
void out(const T& value) { std::cout << value; }
 
out("123");     // P = const T&, A = const char[4] lvalue: deduced T = char[4]
out(std::endl); // P = const T&, A = function template: T is in non-deduced context
7) 形参 P,其 A 是一个花括号初始化列表,但 P 不是 std::initializer_list,对它的引用(可能是 cv 限定的),或者对数组的引用}}
template<class T>
void g1(std::vector<T>);
 
template<class T>
void g2(std::vector<T>, T x);
 
g1({1, 2, 3});     // P = std::vector<T>, A = {1, 2, 3}: T is in non-deduced context
                   // error: T is not explicitly specified or deduced from another P/A
 
g2({1, 2, 3}, 10); // P1 = std::vector<T>, A1 = {1, 2, 3}: T is in non-deduced context
                   // P2 = T, A2 = int: deduced T = int
8) 形参 P,它是一个形参包,并且没有出现在形参列表的末尾
template<class... Ts, class T>
void f1(T n, Ts... args);
 
template<class... Ts, class T>
void f2(Ts... args, T n);
 
f1(1, 2, 3, 4); // P1 = T, A1 = 1: deduced T = int
                // P2 = Ts..., A2 = 2, A3 = 3, A4 = 4: deduced Ts = [int, int, int]
 
f2(1, 2, 3, 4); // P1 = Ts...: Ts is non-deduced context
9) 出现在形参 P 中的模板形参列表,并且其中包含一个包展开,该包展开不在模板形参列表的最末尾
template<int...>
struct T {};
 
template<int... Ts1, int N, int... Ts2>
void good(const T<N, Ts1...>& arg1, const T<N, Ts2...>&);
 
template<int... Ts1, int N, int... Ts2>
void bad(const T<Ts1..., N>& arg1, const T<Ts2..., N>&);
 
T<1, 2> t1;
T<1, -1, 0> t2;
 
good(t1, t2); // P1 = const T<N, Ts1...>&, A1 = T<1, 2>:
              // deduced N = 1, deduced Ts1 = [2]
              // P2 = const T<N, Ts2...>&, A2 = T<1, -1, 0>:
              // deduced N = 1, deduced Ts2 = [-1, 0]
 
bad(t1, t2);  // P1 = const T<Ts1..., N>&, A1 = T<1, 2>:
              // <Ts1..., N> is non-deduced context
              // P2 = const T<Ts2..., N>&, A2 = T<1, -1, 0>:
              // <Ts2..., N> is non-deduced context
(自 C++11 起)
10) 对于数组类型的 P(但不是对数组或指向数组的指针的引用),主要数组边界
template<int i>
void f1(int a[10][i]);
 
template<int i>
void f2(int a[i][20]);    // P = int[i][20], array type
 
template<int i>
void f3(int (&a)[i][20]); // P = int(&)[i][20], reference to array
 
void g()
{
    int a[10][20];
    f1(a);     // OK: deduced i = 20
    f1<20>(a); // OK
    f2(a);     // error: i is non-deduced context
    f2<10>(a); // OK
    f3(a);     // OK: deduced i = 10
    f3<10>(a); // OK
}

在任何情况下,如果类型名称的任何部分是非推导的,则整个类型名称都是非推导语境。但是,复合类型可以同时包含推导和非推导的类型名称。例如,在 A<T>::B<T2> 中,T 由于规则 #1(嵌套名称说明符)是非推导的,而 T2 由于它是同一类型名称的一部分也是非推导的,但在 void(*f)(typename A<T>::B, A<T>) 中,A<T>::B 中的 T 是非推导的(由于相同的规则),而 A<T> 中的 T 是推导的。

[编辑] 从类型中推导

给定一个函数形参 P,它依赖于一个或多个类型模板形参 Ti、模板模板形参 TTi 或非类型模板形参 Ii,以及对应的实参 A,如果 P 具有以下形式之一,则会发生推导

  • cv(可选) T;
  • T*;
  • T&;
  • T&&;
(自 C++11 起)
  • T(可选) [I(可选)];
  • T(可选) (U(可选));
(直到 C++17)
  • T(可选) (U(可选)) noexcept(I(可选));
(自 C++17 起)
  • T(可选) U(可选)::*;
  • TT(可选)<T>;
  • TT(可选)<I>;
  • TT(可选)<TU>;
  • TT(可选)<>.

在上述形式中,

  • T(可选)U(可选) 表示类型或 形参类型列表,它要么递归地满足这些规则,要么是 PA 中的非推导语境,要么是 PA 中的相同非依赖类型。
  • TT(可选)TU(可选) 表示类模板或模板模板形参。
  • I(可选) 表示表达式,它要么是 I,要么是 PA 中的值依赖,要么在 PA 中具有相同的常量值。
  • noexcept(I(可选)) 表示异常规范,其中可能隐式的 noexcept 说明符的操作数满足上面 I(可选) 的规则。
(自 C++17 起)

如果 P 具有包含模板形参列表 <T><I> 的形式之一,则该模板实参列表的每个元素 Pi 与其 A 的对应模板实参 Ai 匹配。如果最后一个 Pi 是包展开,则将其模式与 A 的模板实参列表中的每个剩余实参进行比较。否则未推导出的尾随形参包被推导为空形参包。

如果 P 具有包含函数形参列表 (T) 的形式之一,则该列表中的每个形参 Pi 与 A 的函数形参列表中的对应实参 Ai 进行比较。如果最后一个 Pi 是包展开,则将其声明符与 A 的形参类型列表中的每个剩余 Ai 进行比较。

形式可以是嵌套的并递归处理

  • X<int>(*)(char[6])T* 的一个例子,其中 TX<int>(char[6])
  • X<int>(char[6])T(可选) (U(可选)) 的一个例子,其中 TX<int>,而 Uchar[6]
(直到 C++17)
  • X<int>(char[6])T(可选) (U(可选)) noexcept(I(可选)) 的一个例子,其中 TX<int>Uchar[6],且隐式 noexcept 说明符中的 Ifalse
(自 C++17 起)
  • X<int>TT(可选)<T> 的一个例子,其中 TTX,而 Tint,并且
  • char[6]T(可选) [I(可选)] 的一个例子,其中 Tchar,而 Istd::size_t(6)

无法从非类型模板实参的类型推导出类型模板实参

template<typename T, T i>
void f(double a[10][i]);
 
double v[10][20];
f(v); // P = double[10][i], A = double[10][20]:
      // i can be deduced to equal 20
      // but T cannot be deduced from the type of i
(直到 C++17)

当从表达式推导声明为依赖类型的非类型模板形参 P 对应的实参值时,P 类型中的模板形参会从值的类型中推导出来。

template<long n>
struct A {};
 
template<class T>
struct C;
 
template<class T, T n>
struct C<A<n>> { using Q = T; };
 
typedef long R;
 
typedef C<A<2>>::Q R; // OK: T was deduced to long
                      // from the template argument value in the type A<2>
 
template<auto X>
class bar {};
 
template<class T, T n>
void f(bar<n> x);
 
f(bar<3>{}); // OK: T was deduced to int (and n to 3)
             // from the template argument value in the type bar<3>

类型 T[N]N 的类型是 std::size_t

template<class T, T i>
void f(int (&a)[i]);
 
int v[10];
f(v); // OK: T is std::size_t

函数类型的 noexcept(B) 说明符中 B 的类型是 bool

template<bool>
struct A {};
 
template<auto>
struct B;
template<auto X, void (*F)() noexcept(X)>
struct B<F> { A<X> ax; };
 
void f_nothrow() noexcept;
B<f_nothrow> bn; // OK: X is deduced as true and the type of X is deduced as bool.
(自 C++17 起)

如果函数模板的非类型模板形参在函数形参(也是模板)的模板形参列表中使用,并且推导出了相应的模板实参,则推导出的模板实参的类型(如其封闭模板形参列表所指定的,意味着引用被保留)必须与非类型模板形参的类型完全匹配,除非 cv 限定符被删除,以及模板实参是从数组边界推导出来的——在这种情况下,允许任何整型类型,甚至是 bool,即使它总是会变成 true

template<int i>
class A {};
 
template<short s>
void f(A<s>); // the type of the non-type template param is short
 
void k1()
{
    A<1> a;  // the type of the non-type template param of a is int
 
    f(a);    // P = A<(short)s>, A = A<(int)1>
             // error: deduced non-type template argument does not have the same
             // type as its corresponding template argument
 
    f<1>(a); // OK: the template argument is not deduced,
             // this calls f<(short)1>(A<(short)1>)
}
 
template<int&>
struct X;
 
template<int& R>
void k2(X<R>&);
 
int n;
void g(X<n> &x)
{
    k2(x); // P = X<R>, A = X<n>
           // parameter type is int&
           // argument type is int& in struct X's template declaration
           // OK (with CWG 2091): deduces R to refer to n
}

无法从函数默认实参的类型推导出类型模板形参

template<typename T>
void f(T = 5, T = 7);
 
void g()
{
    f(1);     // OK: calls f<int>(1, 7)
    f();      // error: cannot deduce T
    f<int>(); // OK: calls f<int>(5, 7)
}

模板的模板形参的推导可以使用函数调用中使用的模板特化中的类型

template<template<typename> class X>
struct A {}; // A is a template with a TT param
 
template<template<typename> class TT>
void f(A<TT>) {}
 
template<class T>
struct B {};
 
A<B> ab;
f(ab); // P = A<TT>, A = A<B>: deduced TT = B, calls f(A<B>)

[编辑] 其他语境

除了函数调用和运算符表达式之外,模板实参推导还用于以下情况

auto 类型推导

模板实参推导用于变量的声明中,当从变量的初始化器推导 auto 说明符 的含义时。

形参 P 的获取方式如下:在 T 中,包含 auto 的变量的声明类型,auto 的每次出现都替换为一个虚构的类型模板形参 U,或者,如果初始化是复制列表初始化,则替换为 std::initializer_list<U>。实参 A 是初始化器表达式。在按照上述规则从 PA 推导出 U 后,将推导出的 U 替换到 P 中以获得实际的变量类型

const auto& x = 1 + 2; // P = const U&, A = 1 + 2:
                       // same rules as for calling f(1 + 2) where f is
                       // template<class U> void f(const U& u)
                       // deduced U = int, the type of x is const int&
 
auto l = {13}; // P = std::initializer_list<U>, A = {13}:
               // deduced U = int, the type of l is std::initializer_list<int>

在直接列表初始化(但不是复制列表初始化)中,当从花括号初始化列表推导 auto 的含义时,花括号初始化列表必须只包含一个元素,并且 auto 的类型将是该元素的类型

auto x1 = {3}; // x1 is std::initializer_list<int>
auto x2{1, 2}; // error: not a single element
auto x3{3};    // x3 is int
               // (before N3922 x2 and x3 were both std::initializer_list<int>)
(自 C++11 起)

auto 返回函数

模板实参推导用于 函数 的声明中,当从返回语句推导函数返回类型中 auto 说明符 的含义时。

对于 auto 返回函数,形参 P 的获取方式如下:在 T 中,包含 auto 的函数的声明返回类型,auto 的每次出现都替换为一个虚构的类型模板形参 U。实参 Areturn 语句的表达式,如果 return 语句没有操作数,则 Avoid()。在按照上述规则从 PA 推导出 U 后,将推导出的 U 替换到 T 中以获得实际的返回类型

auto f() { return 42; } // P = auto, A = 42:
                        // deduced U = int, the return type of f is int

如果这样的函数有多个 return 语句,则为每个 return 语句执行推导。所有结果类型必须相同,并成为实际的返回类型。

如果这样的函数没有 return 语句,则在推导时 Avoid()

注意:变量和函数声明中 decltype(auto) 占位符的含义不使用模板实参推导。

(自 C++14 起)

[编辑] 重载决议

模板实参推导在重载决议期间使用,当从候选模板函数生成特化时。 PA 与常规函数调用中的相同

std::string s;
std::getline(std::cin, s);
 
// "std::getline" names 4 function templates,
// 2 of which are candidate functions (correct number of parameters)
 
// 1st candidate template:
// P1 = std::basic_istream<CharT, Traits>&, A1 = std::cin
// P2 = std::basic_string<CharT, Traits, Allocator>&, A2 = s
// deduction determines the type template parameters CharT, Traits, and Allocator
// specialization std::getline<char, std::char_traits<char>, std::allocator<char>>
 
// 2nd candidate template:
// P1 = std::basic_istream<CharT, Traits>&&, A1 = std::cin
// P2 = std::basic_string<CharT, Traits, Allocator>&, A2 = s
// deduction determines the type template parameters CharT, Traits, and Allocator
// specialization std::getline<char, std::char_traits<char>, std::allocator<char>>
 
// overload resolution ranks reference binding from lvalue std::cin
// and picks the first of the two candidate specializations

如果推导失败,或者如果推导成功,但其产生的特化将无效(例如,形参既不是类类型也不是枚举类型的重载运算符),则该特化不包含在重载集中,类似于 SFINAE

[编辑] 重载集的地址

当获取重载集的地址时,包括函数模板,会使用模板实参推导。

函数模板的函数类型是 P目标类型A 的类型

std::cout << std::endl;
 
// std::endl names a function template
// type of endl P =
// std::basic_ostream<CharT, Traits>& (std::basic_ostream<CharT, Traits>&)
// operator<< parameter A =
// std::basic_ostream<char, std::char_traits<char>>& (*)(
//   std::basic_ostream<char, std::char_traits<char>>&
// )
// (other overloads of operator<< are not viable) 
// deduction determines the type template parameters CharT and Traits

在这种情况下,推导应用了一个额外的规则:当比较函数形参 Pi 和 Ai 时,如果任何 Pi 是对 cv 无限定模板形参(“转发引用”)的右值引用,并且相应的 Ai 是左值引用,则 Pi 被调整为模板形参类型(T&& 变成 T)。

如果函数模板的返回类型是占位符(autodecltype(auto)),则该返回类型是非推导语境,并从实例化中确定。

(自 C++14 起)

[编辑] 偏序

模板实参推导在重载函数模板的偏序期间使用。

[编辑] 转换函数模板

模板实参推导在选择用户定义的转换函数模板实参时使用。

A 是作为转换结果所需的类型。 P 是转换函数模板的返回类型。如果 P 是引用类型,则在节的以下部分中使用被引用类型代替 P

如果 A 不是引用类型

a) 如果 P 是数组类型,则使用通过数组到指针转换获得的指针类型代替 P
b) 如果 P 是函数类型,则使用通过函数到指针转换获得的函数指针类型代替 P
c) 如果 P 是 cv 限定的,则顶层 cv 限定符将被忽略。

如果 A 是 cv 限定的,则顶层 cv 限定符将被忽略。如果 A 是引用类型,则推导使用被引用类型。

如果从 PA 的常规推导(如上所述)失败,则还会额外考虑以下替代方案

a) 如果 A 是引用类型,则 A 可以比推导出的 A 具有更多的 cv 限定符;
b) 如果 A 是指针或成员指针类型,则允许推导出的 A 是可以通过限定转换转换为 A 的任何指针
struct C
{
    template<class T>
    operator T***();
};
C c;
 
const int* const* const* p1 = c;
 
// P = T***, A = const int* const* const*
// regular function-call deduction for
// template<class T> void f(T*** p) as if called with the argument
// of type const int* const* const* fails
// additional deduction for conversion functions determines T = int
// (deduced A is int***, convertible to const int* const* const*)
c) 如果 A 是函数指针类型,则允许推导出的 A 是指向 noexcept 函数的指针,可以通过函数指针转换转换为 A
d) 如果 A 是指向成员函数的指针,则允许推导出的 A 是指向 noexcept 成员函数的指针,可以通过函数指针转换转换为 A
(自 C++17 起)

有关转换函数模板的其他规则,请参阅成员模板

[编辑] 显式实例化

模板实参推导用于显式实例化显式特化,以及那些声明符 ID 恰好引用函数模板的特化(例如,friend ostream& operator<< <> (...))的 友元声明 中,如果并非所有模板实参都被显式指定或默认,则使用模板实参推导来确定引用哪个模板的特化。

P 是正被考虑作为潜在匹配项的函数模板的类型,而 A 是来自声明的函数类型。如果没有匹配项或多个匹配项(在偏序之后),则函数声明是不良形式的

template<class X>
void f(X a);        // 1st template f
template<class X>
void f(X* a);       // 2nd template f
template<>
void f<>(int* a) {} // explicit specialization of f
 
// P1 = void(X), A1 = void(int*): deduced X = int*, f<int*>(int*)
// P2 = void(X*), A2 = void(int*): deduced X = int, f<int>(int*)
// f<int*>(int*) and f<int>(int*) are then submitted to partial ordering
// which selects f<int>(int*) as the more specialized template

在这种情况下,推导应用了一个额外的规则:当比较函数形参 Pi 和 Ai 时,如果任何 Pi 是对 cv 无限定模板形参(“转发引用”)的右值引用,并且相应的 Ai 是左值引用,则 Pi 被调整为模板形参类型(T&& 变成 T)。

[编辑] 释放函数模板

模板实参推导用于确定释放函数模板特化是否与给定形式的 placement operator new 匹配。

P 是正被考虑作为潜在匹配项的函数模板的类型,而 A 是释放函数的函数类型,该释放函数将是所考虑的 placement operator new 的匹配项。如果没有匹配项或多个匹配项(在重载决议之后),则不会调用 placement 释放函数(可能会发生内存泄漏)

struct X
{
    X() { throw std::runtime_error(""); }
 
    static void* operator new(std::size_t sz, bool b)   { return ::operator new(sz); }
    static void* operator new(std::size_t sz, double f) { return ::operator new(sz); }
 
    template<typename T>
    static void operator delete(void* ptr, T arg)
    {
        ::operator delete(ptr);
    }
};
 
int main()
{
    try
    {
        X* p1 = new (true) X; // when X() throws, operator delete is looked up
                              // P1 = void(void*, T), A1 = void(void*, bool):
                              // deduced T = bool
                              // P2 = void(void*, T), A2 = void(void*, double):
                              // deduced T = double
                              // overload resolution picks operator delete<bool>
    }
    catch(const std::exception&) {}
 
    try
    {
        X* p1 = new (13.2) X; // same lookup, picks operator delete<double>
    }
    catch(const std::exception&) {}
}

[编辑] 别名模板

别名模板 不会被推导,除非在 类模板实参推导(自 C++20 起)

template<class T>
struct Alloc {};
 
template<class T>
using Vec = vector<T, Alloc<T>>;
Vec<int> v;
 
template<template<class, class> class TT>
void g(TT<int, Alloc<int>>);
g(v); // OK: deduced TT = vector
 
template<template<class> class TT>
void f(TT<int>);
f(v); // error: TT cannot be deduced as "Vec" because Vec is an alias template

[编辑] 隐式转换

类型推导不考虑隐式转换(上面列出的类型调整除外):那是重载决议的工作,它稍后发生。但是,如果所有参与模板实参推导的形参的推导都成功,并且所有未推导出的模板实参都被显式指定或默认,则将剩余的函数形参与相应的函数实参进行比较。对于每个剩余的形参 P,其类型在替换任何显式指定的模板实参之前是非依赖的,如果对应的实参 A 无法隐式转换为 P,则推导失败。

在模板实参推导中没有模板形参参与的依赖类型形参,以及由于显式指定的模板实参的替换而变为非依赖的形参,将在重载决议期间进行检查

template<class T>
struct Z { typedef typename T::x xx; };
 
template<class T>
typename Z<T>::xx f(void*, T); // #1
 
template<class T>
void f(int, T);                // #2
 
struct A {} a;
 
int main()
{
    f(1, a); // for #1, deduction determines T = struct A, but the remaining argument 1
             // cannot be implicitly converted to its parameter void*: deduction fails
             // instantiation of the return type is not requested
             // for #2, deduction determines T = struct A, and the remaining argument 1
             // can be implicitly converted to its parameter int: deduction succeeds
             // the function call compiles as a call to #2 (deduction failure is SFINAE)
}

[编辑] 缺陷报告

以下行为变更缺陷报告已追溯应用于以前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
CWG 70 C++98 未指定是否会推导出数组边界 指定为非推导
CWG 300 C++98 推导发生在以下形式的函数形参中
type(*)(T)/T(*)()/T(*)(T),函数指针
匹配这些形式,但函数引用不匹配
将这些形式更改为
type(T)/T()/T(T),以便它们
也可以覆盖引用
CWG 322 C++98 引用类型的类型形参未
调整为使用被引用类型进行推导
添加了调整
CWG 976 C++98 在转换运算符模板的推导中,
const T& 返回类型永远无法匹配 T 结果类型
调整了规则以
允许此类匹配
CWG 1387 C++11 decltype 说明符的表达式不是非推导语境 现在是了
CWG 1391 C++98 未指定不参与推导的实参的隐式转换效果
指定如上所述
CWG 1591
无法从花括号初始化列表推导出数组边界和元素类型 C++11 允许推导 CWG 2052
推导具有非类 C++98 非枚举实参的运算符是硬错误
如果有其他重载,则是软错误
如果有其他重载,则是软错误
有其他重载
CWG 2091 C++98 由于与实参类型不匹配,推导引用非类型形参不起作用
避免了类型不匹配
避免类型不匹配
N3922 C++11 auto 的直接列表初始化推导出 std::initializer_list 对于多个元素是不良形式的
元素,为单个元素推导元素类型
元素类型
CWG 2355 C++17 函数类型的 noexcept 说明符中的值是不可推导的 变为可推导