Boost logo

Boost Users :

Subject: Re: [Boost-users] [variant] forward declarations
From: Nathan Crookston (nathan.crookston_at_[hidden])
Date: 2014-07-18 15:37:38


Hi Michael,

Michael Powell wrote:

> Hello,
>
> I've got some structs in a AST of sorts which the main player in the
> mix is a node. In at least one of the use cases, one of the variant
> types must be forward declared at the time the node can be declared.
> Possibly one or two other similar use cases.
>
> Error 2 error C2139: 'xml::xelement' : an undefined class is not
> allowed as an argument to compiler intrinsic type trait
> '__is_nothrow_constructible'
> i:\installs\development\boost.org
> \1.55.0\boost_1_55_0\stage\include\boost\type_traits\has_nothrow_constructor.hpp
> 24 1 xml
>
> However, I am bumping into C2139, above.
>
> Is there any way for variant to work with forward declared structs (or
> classes, etc)? Or am I just asking too much from C++?
>

Like undiscriminated unions, variant cannot hold incomplete types. Without
knowing the sizes of its potential members, it wouldn't know the maximum
size it should allocate.

The solution to problems like this is usually to hold a pointer to the
incomplete type (not sure if variant works with move-only types like
std::unique_ptr). Variant has some syntactic sugar to allow recursive
variants (where one child could be another variant of the same type) for
tree-like structures.

HTH,
Nate



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net