Category : static-assert

The standard says that the expression in a static_assert should be a constant expression of type bool. Does that somehow not cover expressions including function parameters known at compile time? The following, for example: #include <array> #include <iostream> constexpr std::array names = {"foo", "bar"}; constexpr const char* name(std::size_t i) noexcept { static_assert(i < names.size(), ""); ..

Read more

How can I programatically implement a concept as if it were an actual "interface"? For example, I was trying recently to write a contigious iterator for a custom container. And because contigious iterators have a lot of traits, I wanted to make sure I didn’t miss anything, so I used a static assert like the ..

Read more

I’m trying to determine at compile time if a specific type is of type std::pair. When I compile the code below, I get the assertion fail on both branches (that is, both "HERE1", and "HERE2"). If I remove the static_asserts and uncomment the prints, I get what I expect: That is "HERE1" for is_pair_type<T::value_type>, and ..

Read more

tl;dr When I try to compile this code, Visual Studio throws a compile time error for very static_assert, regardless of wheter it should, and then also one "active error" for the only one that should. This is an issue with my code, or with Visual Studio? Visual Studio Version Microsoft Visual Studio Community 2019 Version ..

Read more

Suppose I have a class template, Angle<T>, and I want to constrain instantiation to Ts that are floating point types. The SFINAE approach: template <typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true> struct Angle { T m_radians; }; C++20 gives us concepts: template <std::floating_point T> struct Angle { T m_radians; }; Alternatively, I could constrain the type ..

Read more