TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 230 subscribers
Post #25660 22
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 type
idt = void(*)();
//
type
idt 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>;
type
idt 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 (size
t)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
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
More from @r_cpp
  1. Sep 26, 2026Token Sequence Injection & Modern Macros: The Most Game-Changing Compile-Time Feature in C…
  2. Sep 25, 2026myStringStream.str("") Considered Harmful Under C++20 I was recently looking at some (rath…
  3. Sep 20, 2026A clever branch free optimization I'm the developer of memlz which is an extremely fast co…
  4. Sep 15, 2026Inside Boost.PolyCollection https://bannalia.blogspot.com/2026/09/inside-boostpolycollecti…
  5. Sep 11, 2026C++26: Standard Library Hardening Experiments https://www.cppstories.com/2026/hardening-ex…
  6. Sep 11, 2026MSVC C++23: constexpr cmath with LLVM Libc https://devblogs.microsoft.com/cppblog/msvc-c23…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →