C++ Logo

STD-DISCUSSION

Advanced search

Subject: Re: Anonymous unions with non-trivial member types
From: Yongwei Wu (wuyongwei_at_[hidden])
Date: 2020-08-29 06:34:06


Thanks for the reply.

I see now that it is the aggregate initialization part that causes
troubles. The Clang error message had misled me. While it reported:

note: destructor of '' is implicitly deleted
      because variant field 'string_value' has a non-trivial destructor
        string string_value;

It is not exactly the case. If I do not use aggregate initialization,
compilation can pass:

https://gcc.godbolt.org/z/vva8hK

The next question is: Is it standard-conforming to write the code as I
used? I see ICC rejects the code too. If I put ".string_value = …" in
additional braces, ICC seems happy, Clang still fails, but MSVC now fails
too. Overall, no win.

What is the most correct way to write what I intended? Or maybe
constructors are the correct choice here? (Anyway, knowing the trouble is
only caused by the aggregate initialization, I am not that bothered now.)

On Fri, 28 Aug 2020 at 23:39, Thiago Macieira via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> What I hadn't realised and your example below shows is that you can use
> aggregate initialisation for it. Named initialisers are optional.
>
> > int main()
> > {
> > StringIntChar obj{.type = StringIntChar::String,
> > .string_value = "Hello world"s};
>

-- 
Yongwei Wu
URL: http://wyw.dcweb.cn/


STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups