| 1 | // Copyright (c) 2019-2024 Antony Polukhin. |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #include <type_traits> |
| 7 | |
| 8 | #include <boost/pfr/core.hpp> |
| 9 | |
| 10 | template <class T> |
| 11 | struct constrained_template { |
| 12 | constrained_template() = default; |
| 13 | |
| 14 | template < |
| 15 | class U = T, |
| 16 | std::enable_if_t< |
| 17 | std::is_constructible<T, U&&>::value |
| 18 | || sizeof(decltype(T{std::declval<U&&>()})) |
| 19 | , bool> = false> |
| 20 | constexpr constrained_template(U&& val) |
| 21 | : value_{std::forward<U>(val)} |
| 22 | {} |
| 23 | |
| 24 | T value_; |
| 25 | }; |
| 26 | |
| 27 | struct int_element { |
| 28 | int value_; |
| 29 | }; |
| 30 | |
| 31 | struct aggregate_constrained { |
| 32 | constrained_template<short> a; |
| 33 | constrained_template<int_element> b; |
| 34 | }; |
| 35 | |
| 36 | int main() { |
| 37 | static_assert( |
| 38 | std::is_same< |
| 39 | boost::pfr::tuple_element_t<0, aggregate_constrained>, |
| 40 | constrained_template<short> |
| 41 | >::value, |
| 42 | "Precise reflection with template constructors fails to work" |
| 43 | ); |
| 44 | |
| 45 | static_assert( |
| 46 | std::is_same< |
| 47 | boost::pfr::tuple_element_t<1, aggregate_constrained>, |
| 48 | constrained_template<int_element> |
| 49 | >::value, |
| 50 | "Precise reflection with template constructors fails to work" |
| 51 | ); |
| 52 | |
| 53 | short s = 3; |
| 54 | aggregate_constrained aggr{.a: s, .b: 4}; |
| 55 | return boost::pfr::get<1>(val&: aggr).value_.value_ - 4; |
| 56 | } |
| 57 | |