lang/valorcoll.hpp

The following code example is taken from the book
C++20 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2021
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License

// raw code

#include <iostream>
#include <ranges>

template<typename T>
class ValOrColl {
  T value;
public:
  ValOrColl(const T& val)
   : value{val} {
  }
  ValOrColl(T&& val)
   : value{std::move(val)} {
  }

  void print() const {
    std::cout << value << '\n';   
  }

  void print() const @\TB{requires std::ranges::range<T>}@ {
    for (const auto& elem : value) {
      std::cout << elem << ' ';
    }
    std::cout << '\n';   
  }
};