Search⌘ K
AI Features

- Solution

Explore how to manually declare explicit types in Modern C++ for embedded and safety-critical systems. Understand replacing auto keywords with explicit type deductions involving iterators, maps, lambdas, futures, time points, pairs, and tuples. Gain insight into why explicit type usage is important for clarity and safety in embedded programming.

We'll cover the following...

Solution

C++
// C++ 14
#include <chrono>
#include <functional>
#include <future>
#include <initializer_list>
#include <map>
#include <string>
#include <thread>
#include <tuple>
int main(){
std::initializer_list<int> myInts = {1, 2, 3};
std::initializer_list<int>::iterator myIntBegin = myInts.begin();
std::map<int, std::string> myMap = {{1, std::string("one")}, {2, std::string("two")}};
std::map<int, std::string>::iterator myMapBegin = myMap.begin();
std::function<std::string(const std::string&)> func = [](const std::string& a){ return a; };
std::future<std::string> futureLambda = std::async([]{ return std::string("Hello"); });
std::chrono::time_point<std::chrono::system_clock> begin = std::chrono::system_clock::now();
std::pair<int, std::string> pa = std::make_pair(1, std::string("second"));
std::tuple<std::string, int, double, bool, char> tup = std::make_tuple(std::string("second"), 4, 1.1, true, 'a');
}

Explanation

We have changed the code in the exercise and replaced the auto keywords with their explicit deduction types.

  • In line 14, we have defined an ...