What should I do in the following function if the argument type T is wrong?
template <class T>
constexpr inline size_t GetObjectSize(const T & val)
{
if constexpr (std::is_arithmetic<T>::value)
{
return sizeof(val);
}
if constexpr (std::is_class<T>)
{
return 5u;
}
//there should be compile time error.
}
int * p;
//compile time error
GetObjectSize(p);
Possible alternatives are 1) throwing an exception 2) assert 3) static_assert
1) Exception of what type should I throw?
2) it is implementation defined and is not guaranteed to be a costexpr.
3) static_assert(false) always fails independently of T.
switchorifchecks on type is usually an anti-pattern.