Skip to content

std::construct_at() seems to bypass [[clang::require_explicit_initialization]] #141103

@higher-performance

Description

@higher-performance
#include <memory>
#include <utility>

struct S {
    unsigned char v [[clang::require_explicit_initialization]];
};

template <class T, class... U>
constexpr T* my_construct_at(T* ptr, U&&... args) {
  return ::new (static_cast<void*>(ptr)) T(std::forward<U>(args)...);
}

int main() {
    unsigned char buf[sizeof(S)];
    std::construct_at(reinterpret_cast<S*>(buf));
    // my_construct_at(reinterpret_cast<S*>(buf));
}

This code produces:

<source>:10:42: error: field in 'S' requires explicit initialization but is not explicitly initialized [-Werror,-Wuninitialized-explicit-init]
   10 |   return ::new (static_cast<void*>(ptr)) T(std::forward<U>(args)...);
      |                                          ^
<source>:16:5: note: in instantiation of function template specialization 'my_construct_at<S>' requested here
   16 |     my_construct_at(reinterpret_cast<S*>(buf));
      |     ^
<source>:5:19: note: 'v' declared here
    5 |     unsigned char v [[clang::require_explicit_initialization]];
      |                   ^

However, it runs fine if my_construct_at(...) is commented out.

It appears the compiler is treating std::construct_at specially somehow -- either disabling warnings on it, or rewriting it internally in some way. This should not be possible. It breaks the initialization guarantee of [[clang::require_explicit_initialization]].

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions