C++ Logo

std-proposals

Advanced search

Re: Static Failure - Custom Compile-Time Errors

From: David Braun <da.braun1_at_[hidden]>
Date: Fri, 18 Jun 2021 07:26:42 +0000
Yes, the part of my code between the two if-statements marked with /*…*/ was meant to imply that.

Another use case would be if you had a function that could take an array or vector of any arithmetic type, but you needed to do something totally different for each type such as processing with AVX2 Intrinsics.

However you want to prevent the user of your code from passing a specific type such as one that dose not work with AVX2 such as long double or Intel’s _Quad.

Another use case is using if-constexpr to replace macros and preventing the user of your code from passing an invalid macro.

Something like this:

uint16_t option1 = 1;
uint16_t option2 = 2;

template<uint16_t opt>
void myFunc()
{
        if constexpr(opt == option1)
        {
                //Compile option 1
}//End if
else if(opt == option2)
{
        //Compile option 2
}//End if
else
{
        static_failure (“Compile Error! You passed an invalid macro option!”);
}//End else
}//End myFunc

You could use static_assert for a simple example like this but imagine a function with 20 MACRO options (they exist), that would be really annoying to have to type blocks of ORs all through a single static_assert and there’d be high chances of making a mistake. I think this design would be a little nicer and cleaner.

These are just a few examples. I’m sure there are other use cases. The idea is to be able to place static_failure(“”); anywhere in your code you want to trigger a compile-time error.



David Aaron Braun
519-680-9822
226-236-4898
da.braun1_at_[hidden]m<mailto:da.braun1_at_[hidden]>

From: Std-Proposals <std-proposals-bounces_at_[hidden]> On Behalf Of chuanqi.xcq via Std-Proposals
Sent: Thursday, June 17, 2021 10:11 PM
To: std-proposals <std-proposals_at_[hidden]g>
Cc: chuanqi.xcq <yedeng.yd_at_[hidden]>
Subject: Re: [std-proposals] Static Failure - Custom Compile-Time Errors

Hi David,

For the motivation example, it looks same for me to:
```

template<typename T> requires std::is_integral<T>

void my_func(T param) {

static_assert(std::is_same<T, uint8_t>() || std::is_same<T,int64_t>() ,”Compile Error! You passed an

int32_t into my_func !”);

}
```

It looks fine to me. So is the main motivation for static_failure for the situation in which the list of `if constexpr` is extrmely long?

Thanks,
Chuanqi

------------------------------------------------------------------
From:David Braun via Std-Proposals <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>>
Send Time:2021年6月18日(星期五) 09:59
To:std-proposals_at_[hidden] <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>>
Cc:David Braun <da.braun1_at_[hidden]<mailto:da.braun1_at_[hidden]>>
Subject:[std-proposals] Static Failure - Custom Compile-Time Errors

From: David Braun
Sent: Thursday, June 17, 2021 12:29 AM
To: lwgchair_at_[hidden]m<mailto:lwgchair_at_[hidden]>
Subject: C++ 23 Language Proposal for new type of static assertion

Here is my proposal for C++ 23.
Thank You!

David Aaron Braun
519-680-9822
226-236-4898
da.braun1_at_[hidden]<mailto:da.braun1_at_[hidden]>


Received on 2021-06-18 02:26:47