I'm currently writing a library which makes use of C++20's std::span
. Compiler library support for std::span
is at this point rather scarce. Therefore, I have a marco which allows to use a 3rd-party implementation instead (in my case tcb::span
). The macro looks like this:
#ifdef SPAN_BUILTIN
# include <span>
# define SPAN std::span
#else
# include "3rdparty/span.hpp"
# define SPAN tcb::span
#endif
Usage through the library looks like this:
void my_func(const SPAN<int>& s);
This is not pretty at all. I was looking for a better solution and I came across std::conditional
which I have already used in the past. A naive attempt would looks like this:
constexpr const bool span_builtin = // ...
template <class T>
using span_type = typename std::conditional<span_builtin, std::span<T>, tcb::span<T>>::type;
Which would result in usage like this:
void my_func(const span_type& s);
The problem lies in the fact that std::span
is an unknown type at compile time when std::span
is not available which makes compilation fail.
Is there a nicer solution to this?