C++ Logo

std-discussion

Advanced search

Re: Can static_assert(false) be ignored in if constexpr?

From: Richard Hodges <hodges.r_at_[hidden]>
Date: Wed, 13 Jan 2021 17:15:43 +0100
>
>
>
> I wish there was an established solution for this.
>

Another hack. Pick your poison:

https://godbolt.org/z/a6d14b

template<class T>
constexpr bool compile_time_logic_error(T&&)
{
  throw "compile time logic error";
}

template <typename T>
ReturnType Function(T&& arg)
{
  if constexpr(condition_on_<T>) {
    // Something to do
  } else if constexpr(another_condition_on<T>) {
    // Something else to do
  } else {
    constexpr auto _ = compile_time_logic_error(arg);
  }

  // Some common processing regarding arg
  return 0;
}

Received on 2021-01-13 10:15:58