Occasionally, I'll assign the returned value of a function to a variable of auto type (e.g. auto returnValue = someFunction();), but still would like to clarify/enforce certain assumptions about the type of that variable - i.e. That it is of type int.
While Concepts & type_traits provide some very powerful static assumption-verifying features, they don't enable something like this:
static_assert( isType( returnValue, int ) );
//OR
static_assert( int == typeof( returnValue ) );
How can I do this?
You can use type traits ie std::is_same here :
static_assert( std::is_same<int, decltype( returnValue ) >:: value , "Error, Bad Type");
Demo here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With