Title
Clarify when `constant_of` throws
Status
new
Section
[meta.reflection.queries]
Submitter
Marek Polacek

Created on 2026-03-31.00:00:00 last changed 2 weeks ago

Messages

Date: 2026-04-04.15:19:15

Proposed resolution:

This wording is relative to N5032.

  1. Modify [meta.reflection.queries] as indicated:

    consteval info constant_of(info r);
    

    -7- Let R be a constant expression of type `info` such that R == r is `true`. If `r` represents an annotation, then let C be its underlying constant.

    -8- Effects: Equivalent to:

    if constexpr (is_annotation(R)) {
      return C;
    } else if constexpr (is_array_type(type_of(R))) {
      return reflect_constant_array([: R :]);
    } else if constexpr (is_function_type(type_of(R))) {
      return reflect_function([: R :]);
    } else {
      return reflect_constant([: R :]);
    }
    

    -9- Throws: `meta::exception` unless either `r` represents an annotation or [: R :] is a valid splice-expression ([expr.prim.splice]) that doesn't designate a template.

Date: 2026-03-31.00:00:00

The `constant_of` specification in [meta.reflection.queries] p9 says:

Throws: `meta::exception` unless either `r` represents an annotation or [: R :] is a valid splice-expression ([expr.prim.splice]).

But if we have `[:^^X:]` where `X` is a template, the splice-expression is valid, but we should still throw, because `constant_of` on a template declaration makes no sense. Since we call `reflect_constant` on the splice as per:

if constexpr (is_annotation(R)) {
  return C;
} else if constexpr (is_array_type(type_of(R))) {
  return reflect_constant_array([: R :]);
} else if constexpr (is_function_type(type_of(R))) {
  return reflect_function([: R :]);
} else {
  return reflect_constant([: R :]);
}

the code would be ill-formed anyway, but it seems clearer to say something like:

Throws: `meta::exception` unless either `r` represents an annotation or [: R :] is a valid splice-expression that doesn't designate a template.

History
Date User Action Args
2026-04-04 15:19:15adminsetmessages: + msg16252
2026-03-31 00:00:00admincreate