Convert std::variant to another std::variant with super-set of types

This is an implementation of Yakk’s second option: template <class… Args> struct variant_cast_proxy { std::variant<Args…> v; template <class… ToArgs> operator std::variant<ToArgs…>() const { return std::visit([](auto&& arg) -> std::variant<ToArgs…> { return arg ; }, v); } }; template <class… Args> auto variant_cast(const std::variant<Args…>& v) -> variant_cast_proxy<Args…> { return {v}; } You might want to fine tune … Read more

Constexpr if alternative

One of pre-C++17 ways is to use partial template specializations, like here: template <typename T, bool AorB> struct dummy; template <typename T, true> struct dummy { static void MyFunc() { FunctionA<T>(); } } template <typename T, false> struct dummy { static void MyFunc() { FunctionB<T>(); } } template <typename T> void Facade() { dummy<T, MeetsConditions<T>::value>::MyFunc(); … Read more

Contiguous iterator detection

Original answer The rationale is given in N4284, which is the adopted version of the contiguous iterators proposal: This paper introduces the term “contiguous iterator” as a refinement of random-access iterator, without introducing a corresponding contiguous_iterator_tag, which was found to break code during the Issaquah discussions of Nevin Liber’s paper N3884 “Contiguous Iterators: A Refinement … Read more

Is it allowed for a compiler to optimize away a local volatile variable?

No. Access to volatile objects is considered observable behavior, exactly as I/O, with no particular distinction between locals and globals. The least requirements on a conforming implementation are: Access to volatile objects are evaluated strictly according to the rules of the abstract machine. […] These collectively are referred to as the observable behavior of the … Read more

Deduction guides and variadic class templates with variadic template constructors – mismatched argument pack lengths

To simplify your example further, it appears that GCC does not implement variadic template arguments in deduction guides: https://wandbox.org/permlink/4YsacnW9wYcoceDH I didn’t see any explicit mention of variadic templates in the wording for deduction guides in the standard or on cppreference.com. I see no interpretation of the standard that disallows this. Therefore I think this is … Read more

Using std::string_view with api that expects null-terminated string

I solved this problem by creating an alternate string_view class called zstring_view. It’s privately inherited from string_view and contains much of its interface. The principal difference is that zstring_view cannot be created from a string_view. Also, any string_view APIs that would remove elements from the end are not part of the interface or they return … Read more