Files
clang-p2996/libcxx/test/std/experimental/reflection/p2996-ex-selecting-members-pt3.pass.cpp
2025-05-02 11:09:40 -04:00

35 lines
1.0 KiB
C++

//===----------------------------------------------------------------------===//
//
// Copyright 2024 Bloomberg Finance L.P.
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03 || c++11 || c++14 || c++17 || c++20
// ADDITIONAL_COMPILE_FLAGS: -freflection
// ADDITIONAL_COMPILE_FLAGS: -Wno-inconsistent-missing-override
// <experimental/reflection>
//
// [reflection]
#include <experimental/meta>
struct S { unsigned i:2, j:6; };
consteval auto member_named(std::string_view name) {
constexpr auto ctx = std::meta::access_context::current();
for (std::meta::info field : nonstatic_data_members_of(^^S, ctx)) {
if (identifier_of(field) == name) return field;
}
std::unreachable();
}
int main() {
S s{0, 0};
s.[:member_named("j"):] = 42; // Same as: s.j = 42;
}