2
0
mirror of https://github.com/boostorg/pfr.git synced 2026-01-19 04:22:13 +00:00
Files
pfr/test/core/run/template_constructor.cpp
Antony Polukhin 5a48d7456f Rewrite modules following the new recommended Boost practice (#196)
Changes:

1) `#include <boost/pfr...` is now implicitly does `import boost.pfr` if the modules are supported 
2) CI now tests modules on Ubuntu 24.04 with existing runtime tests
3) Renamed module to `boost.pfr`
4) CMakeLists.txt now uses modules for `Boost::pfr` target if modules are supported
5) All the library internals now have unconditional module level linkage. `1)` allows users to mix `#include <boost/pfr...` and `import boost.pfr` in user code without ODR-violations.

Significant differences from https://anarthal.github.io/cppblog/modules3:
* PFR uses a `BOOST_PFR_USE_STD_MODULE` macro for `import std;` / `includes` while building module. This allows to use `boost.pfr` module in C++20 and even without usable  `std` module.
2025-04-16 09:16:09 +03:00

64 lines
1.5 KiB
C++

// Copyright (c) 2019-2025 Antony Polukhin.
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <type_traits>
#include <boost/pfr/core.hpp>
#if defined(BOOST_USE_MODULES) // TODO: fix for BOOST_USE_MODULES
int main() {}
#else
template <class T>
struct constrained_template {
constrained_template() = default;
template <
class U = T,
std::enable_if_t<
std::is_constructible<T, U&&>::value
|| sizeof(decltype(T{std::declval<U&&>()}))
, bool> = false>
constexpr constrained_template(U&& val)
: value_{std::forward<U>(val)}
{}
T value_;
};
struct int_element {
int value_;
};
struct aggregate_constrained {
constrained_template<short> a;
constrained_template<int_element> b;
};
int main() {
static_assert(
std::is_same<
boost::pfr::tuple_element_t<0, aggregate_constrained>,
constrained_template<short>
>::value,
"Precise reflection with template constructors fails to work"
);
static_assert(
std::is_same<
boost::pfr::tuple_element_t<1, aggregate_constrained>,
constrained_template<int_element>
>::value,
"Precise reflection with template constructors fails to work"
);
short s = 3;
aggregate_constrained aggr{s, 4};
return boost::pfr::get<1>(aggr).value_.value_ - 4;
}
#endif