Skip to content

Destroying an object during a call to construct_at erroneously rejected during constant evaluation #61562

@davidstone

Description

@davidstone

The following valid (I think) translation unit

#include <memory>

struct ref_holder {
	constexpr explicit ref_holder(int & value):
		m_value(value)
	{
	}

	constexpr operator int() const {
		using T = int;
		m_value.~T();
		return int();
	}

private:
	int & m_value;
};

constexpr bool f() {
	int x = 0;
	std::construct_at(&x, ref_holder(x));
	return true;
}

static_assert(f());

is rejected by clang with

<source>:25:15: error: static assertion expression is not an integral constant expression
static_assert(f());
              ^~~
<source>:11:3: note: destruction of object outside its lifetime is not allowed in a constant expression
                m_value.~T();
                ^
/opt/compiler-explorer/clang-assertions-trunk-20230319/bin/../include/c++/v1/__memory/construct_at.h:38:50: note: in call to '&ref_holder(x)->operator int()'
  return ::new (std::__voidify(*__location)) _Tp(std::forward<_Args>(__args)...);
                                                 ^
<source>:21:2: note: in call to 'construct_at(&x, ref_holder(x))'
        std::construct_at(&x, ref_holder(x));
        ^
<source>:25:15: note: in call to 'f()'
static_assert(f());
              ^
1 error generated.
Compiler returned: 1

See it live: https://godbolt.org/z/T6Wo6rd6d

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"questionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions