C++ Logo

std-proposals

Advanced search

Relax static_assert message restriction

From: Garrett May <garrett.ls.may_at_[hidden]>
Date: Thu, 4 Jul 2019 21:56:50 +0100
Hi,

C++20 will bring about constraints and concepts, which will provide more
useful messages for diagnosing errors. However, as far as I know, it is
still not possible to throw at compile-time custom error messages built
from compile-time information.

Our current mechanism for doing something along those lines is to use
static_assert, but that requires the provided message to be a string
literal. My suggestion here is to relax this restriction to allow messages
to be a constexpr static char const* type.

With the ability to handle compile-time strings (char const*) much more
easily in C++20, it would then be feasible to do something like the
following:

template<typename T, typename S>
constexpr void foo(){
constexpr auto t_size = sizeof(T);
constexpr auto s_size = sizeof(S);
constexpr bool same = (t_size == s_size);
constexpr static char const* msg = str_lit<"These are not the same",
t_size, s_size>{}; // Build a constexpr static char const* with the
embedded compile-time sizes
static_assert(!same, msg);
}

Is something like this already possible, and if not, does this idea
interest anyone?

Thanks,

Garrett

Received on 2019-07-04 15:58:55