美文网首页
使用auto 和decltype进行类型自动推导

使用auto 和decltype进行类型自动推导

作者: arkliu | 来源:发表于2022-11-08 13:26 被阅读0次

在 C++11 中可以使用 auto 自动推导变量的类型,还能够结合 decltype 来表示函数的返回值

PS:
使用auto声明的变量必须要进行初始化,以让编译器推导出它的实际类型,在编译时将auto占位符替换为真正的类型

auto定义变量

auto 变量名 = 变量值;

\color{red}{当变量不是指针或者引用类型时,推导的结果中不会保留 const、volatile 关键字}
\color{red}{当变量是指针或者引用类型时,推导的结果中会保留 const、volatile 关键字}

实例

int main() {
    // int
    auto i = 5;
    // double
    auto d = 6.6;
    //float
    auto f = 3.4f;


    int ii = 110;
    auto *a = ⅈ   // auto: int
    auto b = ⅈ   // auto: int *  
    auto &c = ii;   // auto: int    
    auto dd = ii;    // auto int

    int tm = 88;
    const auto first = tm;  // const int
    auto second = first;  // int 非指针或引用类型推导的结果中不会保留 const、volatile 关键字

    const auto &third = tm; // const int&
    auto& fourth = third;  // const int&
    auto* fifth = &first;  //  const int*
    return 0;
}

auto不能使用的场景

  • 不能作为函数参数使用。因为只有在函数调用的时候才会给函数参数传递实参,auto 要求必须要给修饰的变量赋值,因此二者矛盾
  • 不能用于类的非静态成员变量的初始化
  • 不能使用 auto 关键字定义数组
  • 无法使用 auto 推导出模板参数

decltype使用

推导规则

  • 表达式为普通变量或者普通表达式或者类表达式,在这种情况下,使用 decltype 推导出的类型和表达式的类型是一致的
  • 表达式是函数调用,使用 decltype 推导出的类型和函数返回值一致,\color{red}{对于纯右值而言,只有类类型可以携带const、volatile限定符,除此之外需要忽略掉这两个限定符}
  • 表达式是一个左值,或者被括号 ( ) 包围,使用 decltype 推导出的是表达式类型的引用(如果有 const、volatile 限定符不能忽略)
int fun() {}
int * funp() {}
const int func_cint() {}

class Test
{
    public:
        int num;
        Test() {}
};

int main() {
    int a = 10;
    decltype(a) b = 99;                 // b -> int
    decltype(a+3.14) c = 52.13;         // c -> double
    decltype(a+b*c) d = 520.1314;       // d -> double

    decltype(fun()) e = 55; // 如果是函数,则推导结果为函数返回值的类型
    decltype(funp()) f = &a;
    // 对于纯右值而言,只有类类型可以携带const、volatile限定符,除此之外需要忽略掉这两个限定符, 结果为int
    decltype(func_cint()) g = 8; 

    const Test test;
    //带有括号的表达式
    decltype(test.num) first = 0;  // int
    decltype((test.num)) second = a; // const int&

    //加法表达式
    int n = 0, m = 0;
    decltype(n + m) cc = 0;  // cc是int类型
    decltype(n = n + m) dd = n; // n是左值 dd是int &类型
    return 0;
}

返回值类型后置

template<typename A, typename B>
auto add(A a, B b) -> decltype(a+b) {
    return a+b;
}

int main() {
    int a = 200;
    double b = 3.14;
    auto ret = add<int, double>(a, b);
    auto ret1 = add(a, b);
    cout << "ret = "<<ret <<"   ret1 = "<<ret1<<endl;
    return 0;
}

相关文章

  • C++11中auto和decltype

    C++11中auto和decltype auto和decltype都是C++11中引进来用于自动推断类型的关键字,...

  • auto类型推导与const

    auto类型推导规则 C++11中新增了使用auto进行自动类型推断的功能,从此使用容器等复杂类型时,可以简化代码...

  • C++11的类型推导详解

    auto & decltype 关于C++11新特性,最先提到的肯定是类型推导,C++11引入了auto和decl...

  • C++11带来的优雅语法

    自动类型推导 auto auto的自动类型推导,用于从初始化表达式中推断出变量的数据类型。通过auto的自动类型推...

  • 模板函数返回类型的演进

    提纲 c++03: trick 方法 c++11: auto->decltype 组合 c++14: 自动推导模板...

  • C++ 11的类型推导 auto

    在声明变量时使用auto代替类型,C++编译器就会自动推导出变量的类型 sum被推导为int,使用auto声明的变...

  • C++11类型推导

    C++11 重新定义了auto 和 decltype 这两个关键字实现了类型推导,让编译器来操心变量的类型。 au...

  • [C++11阅读][3-3-2]decltype类型推导(上)

    表达式推导 如下面的例子,decltype的类型推导并不是像auto一样从变量声明的初始化获得变量类型,而是以一个...

  • C++11 类型推导,auto和decltype

    本文根据众多互联网博客内容整理后形成,引用内容的版权归原始作者所有,仅限于学习研究使用,不得用于任何商业用途。 a...

  • Item 1Understand template type d

    引子 模板类型推导是Modern C++特性auto的基础,但模板类型推导和auto类型推导有一些区别,具体看正文...

网友评论

      本文标题:使用auto 和decltype进行类型自动推导

      本文链接:https://www.haomeiwen.com/subject/eugnzrtx.html