C++ Logo

std-proposals

Advanced search

Re: Static Failure - Custom Compile-Time Errors

From: Jefferson Carpenter <jeffersoncarpenter2_at_[hidden]>
Date: Fri, 18 Jun 2021 08:08:39 +0000
On 6/18/2021 7:49 AM, chuanqi.xcq via Std-Proposals wrote:
> Yeah, another question is that can we use 'static_assert(false, "string");' to replace the use case? I understand the current document and implementation wouldn't work.
> But I mean, what if to change the semantics for static_assert to make `static_assert(false, "")` work for the use case you mentioned? It looks not bad to me, too.

Here are some data to consider:

     template<bool B>
     void func() {
       if constexpr (B) {
         static_assert(false, "message");
       }
     }

and

     template<bool B>
     void func;

     template<>
     void func<true>() {
       static_assert(false, "message");
     }

     template<>
     void func<false>() {}

both fail to compile. So currently static_assert behaves similarly
whether it is used with template specializations, or with `if constexpr`
statements. (Even if the code in question is never instantiated)

Received on 2021-06-18 03:08:52