• 第1章 函数模板:1.2 模板参数的推导


    1.2 Template Argument Deduction

    1.2 模板参数的推导

    When we call a function template such as max() for some arguments, the template parameters are determined by the arguments we pass. If we pass two ints to the parameter types T, the C++ compiler has to conclude that T must be int.

    当我们用实参调用函数模板(如max())时,模板参数可以由我们所传递的实参来决定。如果我们传递了两个int给参数类型T。那么C++编译器能够推断出T必须是int。

    However, T might only be “part” of the type. For example, if we declare max()to use constant references:

    但是,T可能只是该类型的一部分。例如,如果我们将max()形参声明为const引用。

    template<typename T>
    T max (T const& a, T const& b)
    {
        return b < a ? a : b;
    }

    and pass int, again T is deduced as int, because the function parameters match for int const&.

    然后传入int类型,这一次T被推导为int类型。因为函数参数匹配int const&。

    Type Conversions During Type Deduction

    类型推导期间的类型转换

    Note that automatic type conversions are limited during type deduction:

    注意,在类型推导期间,自动类型转换是受到限制的:

        • When declaring call parameters by reference, even trivial conversions do not apply to type deduction. Two arguments declared with the same template parameter T must match exactly.

          当通过引用声明形参时,即使是平凡的转换也不适用于类型推导。使用相同模板参数T声明的两个形参必须完全匹配。

        • When declaring call parameters by value, only trivial conversions that decay are supported:Qualifications with const or volatile are ignored, references convert to the referenced type, and raw arrays or functions convert to the corresponding pointer type. For two arguments declared with the same template parameter T the decayed types must match.

         当通过值类型声明形参时,仅支持平凡的退化(decay)转换: const和volatile限定符将被忽略;引用转换为引用的类型;原始数据和函数转换为相应的指针类型。使用相同模板参数T声明的两个形参,其退化类型必须匹配。

    For example:

    例如:

    template<typename T>
    T max (T a, T b);
    
    …
    
    int const c = 42;
    max(i, c); // OK: T is deduced as int
    max(c, c); // OK: T is deduced as int
    int& ir = i;
    max(i, ir); // OK: T is deduced as int
    int arr[4];
    max(&i, arr); // OK: T is deduced as int*

    However, the following are errors:

    但是,下列是错误的:

    max(4, 7.2);  // ERROR: T can be deduced as int or double
    std::string s;
    max("hello", s); //ERROR: T can be deduced as char const[6] or std::string

    There are three ways to handle such errors:

    这里有三种方法可以处理这些错误:

     1. Cast the arguments so that they both match:

    1.强制转换参数,使他们都匹配:

    max(static_cast<double>(4), 7.2); // OK

    2. Specify (or qualify) explicitly the type of T to prevent the compiler from attempting type deduction:

    2.明确指定(或限定)T的类型,以阻止编译器尝试进行类型推导。

    max<double>(4, 7.2); // OK

    3. Specify that the parameters may have different types.

    3.指明参数可能有多种不同的类型

    Section 1.3 on page 9 will elaborate on these options. Section 7.2 on page 108 and Chapter 15 will discuss the rules for type conversions during type deduction in detail.

    第9页的1.3节将详细介绍这些选项。第108页的7.2节和第15章将详细讨论类型推导期间的类型转换规则。

    Type Deduction for Default Arguments

    默认参数的类型推导

    Note also that type deduction does not work for default call arguments. For example:

    还要注意,类型推导不适用于默认调用参数。例如:

    template<typename T>
    void f(T = "");
    
    …
    
    f(1); // OK: deduced T to be int, so that it calls f<int>(1)
    
    f(); // ERROR: cannot deduce T

    To support this case, you also have to declare a default argument for the template parameter, which will be discussed in Section 1.4 on page 13:

    为了支持这种情况,还必须为模板声明一个默认参数,这将在第13页的1.4节中加以讨论。

    template<typename T = std::string>
    void f(T = "");
    
    …
    
    f (); // OK
  • 相关阅读:
    JS的应用(document对象)
    JS的应用(windows对象二)
    JS的应用(windows对象一)
    递归
    函数使用规则和JS内自带的一些函数
    学习笔记之HTML(十秒倒计时;列表收起、展开;下拉列表;
    学习笔记之大图轮播加圆点
    学习笔记之函数类型,for循环,练习
    学习笔记之for循环if循环小练习
    学习笔记之09for循环8个小练习
  • 原文地址:https://www.cnblogs.com/5iedu/p/12696479.html
Copyright © 2020-2023  润新知