[llvm-bugs] [Bug 47345] New: Constexpr constructor not initializing a union member is not diagnosed

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Aug 28 08:25:53 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=47345

            Bug ID: 47345
           Summary: Constexpr constructor not initializing a union member
                    is not diagnosed
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++14
          Assignee: unassignedclangbugs at nondot.org
          Reporter: maltsevm at gmail.com
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

Consider the following C++14 code:

template <class> struct C {
    union {
      int i;
    };
    constexpr C() {}
};
constexpr C<int> c;

This code is ill-formed because the constructor C does not initialize the data
member i, but Clang fails to diagnose the error.

According to C++14 [dcl.constexpr]/4:
"The definition of a constexpr constructor shall satisfy the following
constraints:
- ...
In addition, either its function-body shall be = delete, or it shall satisfy
the following constraints:
- ...
- if the class is a union-like class, but is not a union, for each of its
anonymous union members having variant members, exactly one of them shall be
initialized;"

[dcl.constexpr]/6:
"If the instantiated template specialization of a constexpr function template
or member function of a class template would fail to satisfy the requirements
for a constexpr function or constexpr constructor, that specialization is still
a constexpr function or constexpr constructor, even though a call to such a
function cannot appear in a constant expression."

GCC produces the following diagnostic:
<source>:7:18: error: 'constexpr C< <template-parameter-1-1> >::C() [with
<template-parameter-1-1> = int]' called in a constant expression

    7 | constexpr C<int> c;

      |                  ^

<source>:5:15: note: 'constexpr C< <template-parameter-1-1> >::C() [with
<template-parameter-1-1> = int]' is not usable as a 'constexpr' function
because:

    5 |     constexpr C() {}

      |               ^

<source>:5:15: error: 'constexpr' constructor for union 'C<int>::<unnamed
union>' must initialize exactly one non-static data member

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200828/d2602669/attachment.html>


More information about the llvm-bugs mailing list