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

Posted zhangrelay

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了蓝桥ROS机器人之现代C++学习笔记2.3类型推导相关的知识,希望对你有一定的参考价值。

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]





 

 

以上是关于蓝桥ROS机器人之现代C++学习笔记2.3类型推导的主要内容,如果未能解决你的问题,请参考以下文章

蓝桥ROS机器人之现代C++学习笔记支持C++17(已完成)

蓝桥ROS机器人之现代C++学习笔记之路径规划

蓝桥ROS机器人之现代C++学习笔记2.5 模板

蓝桥ROS机器人之现代C++学习笔记7.3 期物

蓝桥ROS机器人之现代C++学习笔记资料

蓝桥ROS机器人之现代C++学习笔记3.1 Lambda 表达式