Example Code From Presentations
|
Example code from the "A Tasty Intro to Generic Programming in C++" presentation. More...
#include <algorithm>
#include <vector>
#include <list>
#include <array>
#include <functional>
#include <complex>
#include <string>
#include <type_traits>
#include <tuple>
#include <optional>
#include "decimal.h"
#include "catch2/catch_test_macros.hpp"
#include "catch2/matchers/catch_matchers.hpp"
#include "catch2/matchers/catch_matchers_range_equals.hpp"
Classes | |
struct | bidirectional_iterator_tag |
struct | random_access_iterator_tag |
struct | cnt_cmp |
struct | add_x |
struct | cmp_cnt |
struct | person |
struct | two_items< T1, T2 > |
class | my_opt< T > |
Concepts | |
concept | big_math_capable |
Typedefs | |
using | my_opt_str = my_opt<std::string> |
using | std_opt_str = std::optional<std::string> |
Functions | |
template<typename RAIter > | |
void | sort_alg (RAIter begin, RAIter end, random_access_iterator_tag) |
template<typename Iter > | |
void | sort_alg (Iter begin, Iter end, bidirectional_iterator_tag) |
TEST_CASE ("Types as function overload tags", "[overload_tags]") | |
bool | traditional_comp_func (int a, int b) |
TEST_CASE ("Function call operator overloading", "[function_call_overload]") | |
int | slide_16::add_div_by_3 (int a, int b) |
constexpr float | slide_16::add_div_by_3 (float a, float b) |
TEST_CASE ("Simple arithmetic function", "[simple_arithmetic_function]") | |
template<typename T > | |
constexpr T | slide_17_18::pre_20_add_div_by_3 (T a, T b) |
constexpr auto | slide_17_18::add_div_by_3 (auto a, auto b) |
constexpr auto | slide_17_18::add_sub_div (auto a, auto b) |
TEST_CASE ("Function template", "[function_template]") | |
template<typename T , int SZ> | |
constexpr T * | gen_array () |
TEST_CASE ("Non type template parm intro", "[non_type_template_parm_intro]") | |
TEST_CASE ("Decimal number type, third party", "[decimal_num_type]") | |
template<typename N1 , typename N2 > | |
constexpr N1 | slide_25::add_div_by_3 (N1 a, N2 b) |
TEST_CASE ("Two template parameters, first specified as return type", "[two_temp_parms]") | |
template<typename N1 , typename N2 > | |
constexpr auto | slide_26::add_div_by_3 (N1 a, N2 b) -> decltype((a+b)/3) |
TEST_CASE ("Two template parameters, deduced return type from decltype", "[deduced_return_type]") | |
template<typename T > | |
constexpr std::complex< T > | some_complex_math (std::complex< T > a, T b) |
template<typename C , typename T > requires (std::is_same_v<C, std::complex<T>>) | |
constexpr C | similar_complex_math (C a, T b) |
TEST_CASE ("Function template with requires", "[requires]") | |
void | math_func_1 (big_math_capable auto a) |
template<big_math_capable T> | |
T | math_func_2 (T a, T b) |
TEST_CASE ("Concept, function templates using the concept", "[concept]") | |
template<typename Ctr , typename F > | |
void | traverse (Ctr &container, F func) |
void | square_val (int &x) |
void | incr_char (char &c) |
TEST_CASE ("Traverse function", "[traverse]") | |
bool | other_alg (auto f, person a, person b) |
TEST_CASE ("Lambda, closure", "[lambda_closure]") | |
TEST_CASE ("Pair, typle", "[pair_tuple]") | |
my_opt_str | my_func (bool return_value) |
std_opt_str | std_opt_func (bool return_value) |
TEST_CASE ("Optional type", "[optional_type]") | |
TEST_CASE ("Non-type template parm", "[non-type-template-parm]") | |
Example code from the "A Tasty Intro to Generic Programming in C++" presentation.
This presentation gives a taste of how types and generic programming are used in C++.
Types are fundamental to C++, and while not a "strictly" typed language, C++ strives to be as strongly typed as possible. Types are used in many ways.
Templates are fundamental to the generic design capabilities of C++, whether as function templates or class templates.The capabilities of templates go beyond simple substitution of types.
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.txt or copy at https://www.boost.org/LICENSE_1_0.txt)