Skip to content

[dcl.pre] In a static_assert message, data() often does not produce a core constant expression #350

@cor3ntin

Description

@cor3ntin

P2741R3 user-generated static_assert messages specifies that

if the static_assert-message is a conditional-expression M,
M.size() shall be a converted constant expression of type std::size_t and let N
denote the value of that expression,
M.data(), implicitly converted to the type ”pointer to const char”, shall be a core
constant expression and let D denote the converted expression

However, consider

struct array {
    constexpr unsigned long size() const {
        return 2;
    }
    constexpr const char* data() const {
        return d_;
    }
    const char d_[2];
};
static_assert(false, array{'a', 'b'});

This static_assert message does not (but should) satisfy the requirements.
The issue here is that std::array<char, 2>{'a', 'b'}.data() produces a pointer that contains the address of an object that does not have static storage duration. In fact it returns a pointer to a subobject of a...temporary?

[dcl.pre] Should establish that

  • the expression M produces a temporary object that is usable in constant expression (and whose lifetime ends when the message is produced?)
  • Maybe not say anything about M.data() being a constant expression - only M.data()[N] which definitely needs to be a constant expression.
  • Alternatively, establish that all of these evaluations happen in the same context, although this seems more complicated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions