Modern c++: operations on function pointers will break the constexpr function.
I'm on MSVC/c++23.
I used some trick to generate type id at compile time:
export template<typename... T> void typeid() {}
export using typeidt = void(*)();
//
typeidt oneid = typeid<int>;
I’ve found that whenever I try to cast a \`type\id` value to an integer or simply perform a magnitude comparison within a `constexpr` function (even when the call chain is entirely resolved at compile time), the function ceases to be `constexpr`, or MSVC simply ignores the relevant code.
template<typename A, typename B>
constexpr auto someconstexprfunction()
{
typeidt a = typeid<A>;
typeidt b = typeid<B>;
//Total if block will be ignored, and the function will not be constexpr anymore.
if (a < b)
{
//discard
}
else
{
//discard
}
//cast the pointer to sizet then the function will not be constexpr anymore.
return (sizet)a;
}
How to fixed the case?
Or another type id solution at compile time will be nice(I have try so many implementation, none of them work correctly in compile time).
https://redd.it/1v6clnl
@r_cpp
Post #25660
22