1

Is there a workaround to do something like this?

if constexpr (std::floating_point<T>) {}
else if constexpr (std::integral<T>) {}
...
else static_failure("Feature expansion needed");

because if I replace static_failure with static_assert, it needs the replication of all the above conditions (they are many and complicated) and it becomes ugly.

// It does not work properly: always fail
else static_assert(false, "Feature expansion needed");
// works fine, but it is ugly
else static_assert(std::floating_point<T> || std::integral<T> || ... || ... || ..., "Feature expansion needed");

I do not prefer a runtime behavior like:

throw "Feature expansion needed";
5
  • it needs the replication of all the above conditions What do you mean? Commented Apr 10, 2023 at 10:04
  • 1
    It doesn't need replication of all the above conditions, and you may as well use always_false<T> as workaround to static_assert issues. That's being said, you'd better put the full requirement as a requirement in the declaration of the function rather than use static_assert to notify that these conditions fail. Commented Apr 10, 2023 at 10:12
  • @kiner_shah Added answer to your question. Commented Apr 10, 2023 at 10:12
  • 1
    Here's the trick. In C++23 you can just use static_assert(false). Commented Apr 10, 2023 at 10:21
  • @n.m. Wow! The correct answer is yours! But g++ does not support it yet. open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2593r0.html Commented Apr 10, 2023 at 10:28

3 Answers 3

5

Yes, you just need the static assert to be dependant on T and always be false like this:

if constexpr (std::is_integral_v<T>) {}
else
    static_assert(sizeof(T) < 0, "Only works for integral T");

https://godbolt.org/z/Yhesd58rK

Sign up to request clarification or add additional context in comments.

1 Comment

static_assert(sizeof(T) < 0); is pedantically ill-formed NDR as static_assert(false); even if compilers fail to diagnose the former and succeed for the later.
3

replace your last if constexpr with static_assert.

Instead of:

else if constexpr (last_case)
     //Do last case
else
     static_failure("prompt");

Do this:

else {
     static_assert(last_case,"prompt");
     //Do last case
};

Some proposals are made to legalize static_assert(false, "prompt");, but it is not yet part of std.

Comments

1

In C++23, you might use static_assert(false); in non-instantiated context. Before, it would be ill-formed NDR, but most compiler do diagnostic on that.

if constexpr (std::floating_point<T>) { /*...*/ }
else if constexpr (std::integral<T>) { /*...*/ }
/*...*/
else static_assert(false, "Feature expansion needed");

Before C++23, you have to cheat, common legal way is with always_false:

template <typename T>
struct always_false : std::false_type {};

and then

if constexpr (std::floating_point<T>) { /*...*/ }
else if constexpr (std::integral<T>) { /*...*/ }
/*...*/
else static_assert(always_false{}, "Feature expansion needed");

1 Comment

About C++23, unfortunately static_assert(false, ...) is not supported as of g++12.2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.