//******************************************************** // The following code example is taken from the book // C++20 - The Complete Guide // by Nicolai M. Josuttis (www.josuttis.com) // http://www.cppstd20.com // // The code is licensed under a // Creative Commons Attribution 4.0 International License // http://creativecommons.org/licenses/by/4.0/ //******************************************************** #include #include #include #include template constexpr auto modifiedAvg(const T& rg) { using elemType = std::ranges::range_value_t; // initialize compile-time vector with passed elements: std::vector v{std::ranges::begin(rg), std::ranges::end(rg)}; // perform several modifications: v.push_back(elemType{}); std::ranges::sort(v); auto newEnd = std::unique(v.begin(), v.end()); //... // return average of modified vector: auto sum = std::accumulate(v.begin(), newEnd, elemType{}); return sum / static_cast(v.size()); }