蓝桥ROS机器人之现代C++学习笔记2.3类型推导

本文深入探讨了C++17中的几个重要特性,包括使用`auto`简化变量声明,`decltype`获取表达式的类型,以及函数返回类型的尾部推导。示例代码展示了如何在不同场景下应用这些特性,以提高代码的简洁性和效率。

auto

#include <initializer_list>
#include <vector>
#include <iostream>

class MagicFoo {
public:
    std::vector<int> vec;
    MagicFoo(std::initializer_list<int> list) {
        for (auto it = list.begin(); it != list.end(); ++it) {
            vec.push_back(*it);
        }
    }
};

int add(auto x, auto y) { // Supported in C++20
    return x+y;
}

int main() {
    MagicFoo magicFoo = {1, 2, 3, 4, 5};
    std::cout << "magicFoo: ";
    for (auto it = magicFoo.vec.begin(); it != magicFoo.vec.end(); ++it) {
        std::cout << *it << ", ";
    }
    std::cout << std::endl;

    auto i = 5;                    // type int
    auto j = 6;                    // type int
    std::cout << add(i, j) << std::endl;

    auto arr = new auto(10);       // type int*
    // auto auto_arr2[10] = {arr}; // invalid
    return 0;
}

g++ -std=c++17 2.06.auto.cpp -o auto


decltype

#include <iostream>
#include <type_traits>

int main() {
    auto x = 1;
    auto y = 2;
    decltype(x+y) z = 3;
    if (std::is_same<decltype(x), int>::value)
        std::cout << "type x == int" << std::endl;
    if (std::is_same<decltype(x), float>::value)
        std::cout << "type z == float" << std::endl;
    if (std::is_same<decltype(x), decltype(z)>::value)
        std::cout << "type z == type x" << std::endl;
    return 0;
}

 g++ -std=c++17 2.07.decltype.cpp -o decltype


尾返回类型推导

#include <iostream>
#include <type_traits>

// before c++11
template<typename R, typename T, typename U>
R add(T x, U y) {
    return x + y;
}
// after c++11
template<typename T, typename U>
auto add2(T x, U y) -> decltype(x+y){
    return x + y;
}
// after c++14
template<typename T, typename U>
auto add3(T x, U y){
    return x + y;
}

int main() {

    // before c++11
    int z = add<int, int, int>(1, 2);
    std::cout << z << std::endl;

    // after c++11
    auto w = add2<int, double>(1, 2.0);
    if (std::is_same<decltype(w), double>::value) {
        std::cout << "w is double: ";
    }
    std::cout << w << std::endl;

    // after c++14
    auto q = add3<double, int>(1.0, 2);
    std::cout << "q: " << q << std::endl;

    return 0;
}

 g++ -std=c++17 2.08.tail.return.type.cpp -o tail


decltype(auto)

template<int i>
struct Int {};

constexpr auto iter(Int<0>) -> Int<0>;

template<int i>
constexpr auto iter(Int<i>) {
    return iter(Int<i-1>{});
}

int main() {
    decltype(iter(Int<10>{})) a;
}

g++ -std=c++17 2.09.decltype.auto.cpp -o decltypeauto


shiyanlou:2/ (master) $ g++ -std=c++17 2.06.auto.cpp -o auto         [16:01:20]
shiyanlou:2/ (master*) $ ./auto                                      [16:01:41]
magicFoo: 1, 2, 3, 4, 5, 
11
shiyanlou:2/ (master*) $ g++ -std=c++17 2.07.decltype.cpp -o decltype
shiyanlou:2/ (master*) $ ./decltype                                  [16:06:28]
type x == int
type z == type x
shiyanlou:2/ (master*) $ g++ -std=c++17 2.08.tail.return.type.cpp -o tail      
shiyanlou:2/ (master*) $ ./tail                                      [16:10:03]
3
w is double: 3
q: 3
shiyanlou:2/ (master*) $ g++ -std=c++17 2.09.decltype.auto.cpp -o decltypeauto
2.09.decltype.auto.cpp:13:16: warning: inline function \u2018constexpr Int<0> iter(Int<0>)\u2019 used but never defined
 constexpr auto iter(Int<0>) -> Int<0>;
                ^
shiyanlou:2/ (master*) $ ./decltypeauto                              [16:14:03]
shiyanlou:2/ (master*) $                                             [16:15:03]





 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangrelay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值