Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can virtual functions be constexpr?

Tags:

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

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

be constexpr?

like image 599
Ralph Tandetzky Avatar asked Jan 16 '16 14:01

Ralph Tandetzky


People also ask

Can constexpr functions be virtual?

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

Can a function return constexpr?

A constexpr function is a function that can be invoked within a constant expression. A constexpr function must satisfy the following conditions: It is not virtual. Its return type is a literal type.

Can a function parameter be constexpr?

We allow annotating a function parameter with constexpr with the same meaning as a variable declaration: must be initialized with a constant expression. We add a new keyword, maybe_constexpr , that deduces whether the parameter is known at compile time.


Video Answer


1 Answers

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

like image 91
Kerrek SB Avatar answered Sep 24 '22 02:09

Kerrek SB