前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >蓝桥ROS机器人之现代C++学习笔记2.3类型推导

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

作者头像
zhangrelay
发布2022-04-29 19:33:19
1930
发布2022-04-29 19:33:19
举报

auto

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

尾返回类型推导

代码语言:javascript
复制
#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)

代码语言:javascript
复制
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

代码语言:javascript
复制
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]
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-03-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档