r/cpp • • Sep 14 '26

Push-based vs Pull-based Customization

https://brevzin.github.io/c++/2026/09/14/push-vs-pull/

Emulating Rust attributes with reflection (or at least trying to).

48 Upvotes

10 comments sorted by

View all comments

2

u/TheoreticalDumbass :illuminati: Sep 15 '26 edited Sep 15 '26

Suppose we had annotation hook on_class_completion_request()

Example:

struct Annotation {
  // called when the annotated class is incomplete, and we need it to become complete
  consteval void on_class_completion_request(std::meta::info type) const {
    queue_injection(^^{
      struct \(type) {
        consteval { __builtin_constexpr_diag(0, "", "defined!"); }
        float x, y, z;
    });
  }
};

struct [[=Annotation{}]] Vector3;

static_assert(sizeof(Vector3) == 12); // calls hook, defines class, prints "defined!"

Let us also assume hooks on_class_declaration() and on_class_template_declaration()

Then we might be able to do:

struct tuple_size_fixer_upper {
  consteval void on_class_completion_request(std::meta::info type) const {
    auto param = template_arguments_of(type)[0]; // tuple_size<T> -> T
    queue_injection(^^::std, ^^{
      template<>
      struct tuple_size<\(param)> :
        integral_constant<size_t, size(\(param)::tuple_elements)> {};
    });
  }
};

struct inject_bindings_t {
  consteval void on_class_declaration(std::meta::info type) const {
    queue_injection(^^::std, ^^{
      template<>
      struct [[=tuple_size_fixer_upper{}]] tuple_size<\(type)>; // intentionally incomplete
    });
  }

  consteval void on_class_template_declaration(std::meta::info tmpl) const {
    queue_injection(^^::std, ^^{
      template<typename T> // should be the universal params stuff, cannot be bothered
      struct [[=tuple_size_fixer_upper{}]] tuple_size<\(tmpl)<T>> :
        integral_constant<size_t, size(\(tmpl)<T>::tuple_elements)> {};
    });
  }
};
inline constexpr inject_bindings_t inject_bindings;

template<typename T>
struct [[=inject_bindings]] wide_result {
  T low, high;
  static constexpr std::meta::info tuple_elements[] = {^^low, ^^high};
  
  struct [[=inject_bindings]] inner {
    float x, y, z;
    static constexpr std::meta::info tuple_elements[] = {^^x, ^^y, ^^z};
  };
};

// what we generated for now:
namespace std {
template<typename T>
struct tuple_size<wide_result<T>> :
  integral_constant<size_t, size(wide_result<T>::tuple_elements)> {};
} // namespace std
// end

// instantiates tuple_size<wide_result<int>>
// which instantiates wide_result<int>
// which sees wide_result<int>::inner declaration
// which emits `struct tuple_size<wide_result<int>::inner>;` specialization
static_assert(std::tuple_size_v<wide_result<int>> == 2);

// new generated:
namespace std {
template<>
struct [[=tuple_size_fixer_upper{}]] tuple_size<wide_result<int>::inner>;
} // namespace std
// end

// tuple_size<wide_result<int>::inner> is incomplete, calls hook
// hook generates definition specialization
static_assert(std:tuple_size_v<wide_result<int>::inner> == 3);

// new generated:
namespace std {
template<>
struct tuple_size<wide_result<int>::inner> :
  integral_constant<size_t, size(wide_result<int>::inner::tuple_elements)> {};
} // namespace std
// end

Problem with classes is they can be templated and not templated, this definition deferring via on_class_completion_request() is needed for templated classes (IIUC).