Skip to content

Bad example in [expr.const]p7 #1627

@zygoloid

Description

@zygoloid

[expr.const]p7 says:

"""
If an expression of literal class type is used in a context where an integral constant expression is required, then that expression is contextually implicitly converted (Clause 7) to an integral or unscoped enumeration type and the selected conversion function shall be constexpr. [ Example:

struct A {
  constexpr A(int i) : val(i) { }
  constexpr operator int() const { return val; }
  constexpr operator long() const { return 43; }
private:
  int val;
};
template<int> struct X { };
constexpr A a = 42;
X<a> x;     // OK: unique conversion to int
int ary[a];   // error: ambiguous conversion

— end example ]
"""

This example appears to be unrelated to the rule in question.

In X<a>, a is implicitly converted to int, and the rule does not apply.
In int ary[a], a is implicitly converted to size_t, and the rule does not apply.

Neither case is a context in which an integral constant expression is required; those are alignas(E) and bit-widths for bit-fields (and now also the implicit std::tuple_size<T>::value in structured bindings). We should probably use one of those in this example instead.

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