C++ Logo


Advanced search

Re: std::is_constexpr_constructible (/ std::is_constexpr)

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sun, 24 Oct 2021 09:57:28 -0700
On Saturday, 23 October 2021 12:57:55 PDT Michael Scire via Std-Proposals
> if constexpr (is_constexpr_constructible_v<Derived>) {
> constexpr ClassTokenType ClassToken = d{}.GetClassToken(); //
> Valid if d is constexpr constructible and has constexpr virtual
> implementation of GetClassToken().
> obj->InitializeClassTokenCache(ClassToken); // This sets the
> class token cache without having to perform a virtual call at runtime.
> } else {
> obj->InitializeClassTokenCache(obj->GetClassToken()); // This
> performs a virtual call in the process of getting the class token.
> }

How do you know that the Derived class hasn't been itself derived from and the
virtual method overridden again?

class MyObject : public SlabAllocatable<MyObject, BaseObject> { /* ... */ };
class MyObject2 : public MyObject { /* ... */ };

Also, virtual destructors aren't constexpr. So unless you're violating The
First Rule of Virtual (the destructor is virtual), your condition is a
constant false. Though I don't see why you need virtuals if you're using CRTP,
so maybe those allocatable objects don't have virtuals and thus can be

Anyway, wouldn't it be better to have a static constexpr token getter method
that you can do if constexpr (requires {...}) on?

Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DPG Cloud Engineering

Received on 2021-10-24 11:57:35