C++ Logo

std-proposals

Advanced search

Re: Allow virtual base classes in constexpr

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Mon, 10 Jun 2019 08:52:16 -0400
Hi Lyberta,

My kneejerk reaction was "of course virtual bases should be supported in
constexpr, if any inheritance is supported in constexpr."
But I figured there must be a reason the original paper omitted virtual
base support, so I went looking. The "support virtual calls in constexpr"
paper was by Peter Dimov.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1064r0.html
The name "Peter Dimov" reminded me that Peter was also the author of a
paper proposing constexpr support for virtual destructors:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1077r0.html
(My understanding is that P1077 is defunct: its ideas about "trivial"
virtual destructors were superseded (unfortunately IMHO) by P0784's ideas
about "non-trivial, yet still constexpr" destructors.)
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0784r3.html

Thinking about destructors reminded me that paper C++ is wishy-washy on
exactly when (and even exactly *how many times*, IIRC) the destructor of a
virtual base subobject will be called. A grandchild class following the
Dreaded Diamond pattern might destroy its grandparent virtual base
subobject during the destructor of the first parent, or during the
destructor of the second parent, or after both, or even (IIRC) in the
destructors of both parents. It seems like a terrible idea to carry over
this wishy-washy behavior into compile-time evaluation.

So my hunch is that the problem has something to do with virtual base
subobject destruction, and that's where I would advise you to look in the
Standard and flesh out any possible problems and think about appropriate
solutions. You might also want to contact paper authors Peter Dimov and
Daveed Vandevoorde and ask what's the problem that caused Daveed to forbid
virtual bases in P0784. (I'd love to know the answer if you do find out!)

HTH,
–Arthur


On Fri, May 31, 2019 at 7:24 PM Lyberta via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> I have a serialization library that is designed very similar to standard
> streams. So I have a very base class and virtually inherit input and
> output streams from it. Naturally, most of the functions are virtual.
>
> When I heard that virtual functions are allowed in constexpr in c++20, I
> was excited because I thought I would be able to make most of the code
> constexpr. Especially span streams that don't use any dynamic allocation.
>
> When GCC 9 was released a month ago with support for virtual constexpr,
> the first thing I tried is to aggressively add constexpr to my library.
> I was immediately stopped by compiler because virtual base classes are
> not allowed.
>
> There is a proposal for text streams that use std::span - p0448. I think
> if we allow constexpr virtual bases, we can have constexpr span streams
> in p0448.
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> http://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2019-06-10 07:54:15