C++ Logo

STD-DISCUSSION

Advanced search

Subject: Re: Anonymous unions with non-trivial member types
From: Thiago Macieira (thiago_at_[hidden])
Date: 2020-08-28 10:38:24


On Friday, 28 August 2020 04:35:26 PDT Yongwei Wu via Std-Discussion wrote:
> I am aware I am using some C++20 features here. The key question is only
> about anonymous unions. Can they have non-trivial member types, as in the
> code? Is the compilation problem a regression bug in Clang?

There's nothing forbidding you from doing what you've done. Just note that the
anonymous union's default constructor will be deleted. And since it's
anonymous, you can't declare an explicit one.

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};

Simple proof: https://gcc.godbolt.org/z/7z9een

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DPG Cloud Engineering

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

Older Archives on Google Groups