• Forum
  • Lounge
  • "Is there any way to add functions to st

 
"Is there any way to add functions to std?" revisited

Re: http://www.cplusplus.com/forum/beginner/281870/

I recently purchased Beginning C++: From Novice to Professional (https://www.amazon.com/gp/product/1484258835/). I found the github repository, after a bit of flailing around, where the source code is located. https://github.com/Apress/beginning-cpp20.

While poking around the code snippets I noticed there are workaround custom written header for the <fmt> and <ranges> standard libraries, for compilers not yet fully implementing C++20 at the time the book and source were written. Examining either of the headers shows the authors DO add to the std namespace.

From the books <ranges> workaround (https://github.com/Apress/beginning-cpp20/blob/main/Workarounds/ranges):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Compatibility include for compilers that do not support the <ranges> module yet
// (falls back to the reference implementation from https://github.com/ericniebler/range-v3)
// See Appendix A for the preprocessing directives and macros we use here.
#ifdef __cpp_lib_ranges
	#error Remove/rename this compatibility header (use Standard Library header instead)
#else
	#include "range-v3/include/range/v3/all.hpp"

	// Technically you are not allowed to add to the std namespace, 
	// but necessity knows no law...
	namespace std::ranges
	{ 
		using namespace ::ranges::cpp20;

		using ::ranges::empty;
		using ::ranges::begin;
		using ::ranges::end;
		using ::ranges::size;

		template <class Val, class CharT, class Traits>
		auto istream_view(basic_istream<CharT, Traits>& s) { return basic_istream_view<Val>(s); }
	}
	namespace std::views
	{
		using namespace ::ranges::cpp20::views;
	}
#endif 

M'ok.

"Luckily" Visual Studio 2019/2022 do both appear to implement all of the C++20 standard (https://en.cppreference.com/w/cpp/compiler_support/20), though for at least a few of the features the language standard needed is -std:c++latest instead of -std:c++20. <ranges> (<concepts> actually) and module use are two C++20 features I know of requiring setting to latest.
Last edited on
The workaround for <fmt> for non-C++20 compilers (https://github.com/Apress/beginning-cpp20/blob/main/Workarounds/format):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Compatibility include for compilers that do not support the <format> module yet
// (falls back to the reference implementation from https://fmt.dev)
// See Appendix A for the preprocessing directives and macros we use here.
#ifdef __cpp_lib_format
	#error Remove Workarounds/format from your include directories (use Standard Library header instead)
#else
	#define FMT_HEADER_ONLY
	#include "fmt/include/fmt/format.h"

	// Technically you are not allowed to add to the std namespace, 
	// but necessity knows no law...
	namespace std 
	{ 
		using namespace fmt;
	}
#endif 
Topic archived. No new replies allowed.