45

Can virtual functions like X::f() in the following code

struct X 
{
    constexpr virtual int f() const 
    {
        return 0;
    }
};

be constexpr?

5
  • 3
    Think about it a minute. It would completely defeat the constexpr purpose. Commented Jan 16, 2016 at 14:37
  • Such a hypothetical function could be used as a constant expression only if the complete type of the calling instance is known to be X. This would essentially require the language to specify "devirtualization rules". Commented Jan 16, 2016 at 14:40
  • 2
    @πάνταῥεῖ In D you can do compile time function evaluation with virtual functions. So it's not unthinkable. Commented Jan 16, 2016 at 14:47
  • 2
    Well, I think it would make sense at least for final functions to be able to be constexpr. Commented Oct 22, 2016 at 13:35
  • 1
    Not just for final methods. It could be useful for a compile time unit test. You instantiate a derived class on the stack and use a static_assert on the result of a virtual method. At that point the whole function body could be visible to the compiler. Commented Jan 31, 2019 at 9:35

4 Answers 4

42

Up through C++17, virtual functions could not be declared constexpr. The general reason being that, in constexpr code, everything happen can at compile time. So there really isn't much point to having a function which takes a reference to a base class and calls virtual functions on it; you may as well make it a template function and pass the real type, since you know the real type.

Of course, this thinking doesn't really work as constexpr code becomes more complex, or if you want to share interfaces between compile-time and runtime code. In both cases, losing track of the original type is easy to do. It would also allow std::error_code to be more constexpr-friendly.

Also, the fact that C++20 will allow us to do (limited) dynamic allocation of objects means that it is very easy to lose track of the original type. You can now create a vector<Base*> in constexpr code, insert some Derived class instances into it, and pass that to a constexpr function to operate on.

So C++20 allows virtual functions to be declared constexpr.

Sign up to request clarification or add additional context in comments.

3 Comments

Ooh, interesting; I actually just ran into a case where compile-time polymorphism would be really useful yesterday, while attempting to set up a compile-time tag list of potentially variable length to aid with other compile-time tasks. Using a const reference to an empty base to allow array size to be a NTTP on the actual tag list was a relatively clean way to solve one of the issues, but introduced its own problems due to the lack of constexpr virtual. Glad to see this'll be available soon!
@JustinTime NTTP?
@curiousguy: Non-type template parameter.
41

This answer is no longer correct as of C++20.

No. From [dcl.constexpr]/3 (7.1.5, "The constexpr specifier"):

The definition of a constexpr function shall satisfy the following requirements:

— it shall not be virtual

7 Comments

It is weird, for gcc version 4.9.2 20141101 (Red Hat 4.9.2-1) (GCC) it works
@zaratustra: And GCC 6 doesn't :-(
@CiroSantilli华涌低端人口六四事件法轮功: "virtual" means "at runtime". "constexpr" means "at compile time".
polymorphic constexpr object with its state could be determined at compile time
Note that virtual constexpr will be legal in c++20. see en.cppreference.com/w/cpp/language/constexpr
|
21

Can virtual functions be constexpr?

Yes. Only since C++20, virtual functions can be constexpr.

Comments

2

In C++20 and later, a constexpr function can be virtual, and a constructor can be defined as constexpr when the enclosing class has any virtual base classes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.