CETL 0.0.0
 
Loading...
Searching...
No Matches
Example 1: CETL C++20 Polyfill Header

Full example for cetl/pf20/cetlpf.hpp

#include <iostream>
#include <string>
#include <algorithm>
#include <type_traits>
#include <gtest/gtest.h>
TEST(example_01_polyfill_20, span_static)
{
std::string greeting{"Hello Dynamic World."};
// the cetl/pf20/cetlpf.hpp header will automatically define cetl::span
// based on the C++ standard available to the compiler. If < 20 then
// it will be the CETL version, if >= 20 then it will be the std version.
cetl::span<const char> dynamic{greeting.c_str(), 13};
auto print = [](const char c) { std::cout << c; };
// Print just the characters in the span...
std::for_each(dynamic.begin(), dynamic.end(), print);
// or...
std::string substring{dynamic.begin(), dynamic.end()};
std::cout << substring << std::endl;
}
namespace
{
//Define these in a header or something...
constexpr std::size_t my_dynamic_extent = cetl::pf20::dynamic_extent;
template <typename T, std::size_t Extent = my_dynamic_extent>
}// namespace
TEST(example_01_polyfill_20, example_01_polyfill_20_span_dynamic)
{
std::string greeting{"Hello Dynamic World."};
// now use my_span instead of cetl::span and you only have one place
// to change to std::span when you upgrade your compiler.
my_span<const char> dynamic{greeting.c_str(), 13};
auto print = [](const char c) { std::cout << c; };
// Print just the characters in the span...
std::for_each(dynamic.begin(), dynamic.end(), print);
// or...
std::string substring{dynamic.begin(), dynamic.end()};
std::cout << substring << std::endl;
}
A borrowed view into a contiguous set of objects.
Definition span.hpp:55
T endl(T... args)
T for_each(T... args)
constexpr std::size_t dynamic_extent
Used by span to indicate that the span size is not fixed.
Definition span.hpp:31
CETL polyfill header for C++20 types.
Defines a span type that is mostly compliant to ISO/IEC 14882:2020(E) but compatible with C++14 and n...