Created
August 25, 2016 01:54
-
-
Save utilForever/1a058050b8af3ef46b58bcfa01d5375d to your computer and use it in GitHub Desktop.
Simple reflection on C++17 [Very simple approach to convert any struct (up to N members) to a tuple using C++17 structured bindings and the idea from Boost.DI]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <tuple> | |
#include <type_traits> | |
#include <cassert> | |
template <class T, class... TArgs> decltype(void(T{std::declval<TArgs>()...}), std::true_type{}) test_is_braces_constructible(int); | |
template <class, class...> std::false_type test_is_braces_constructible(...); | |
template <class T, class... TArgs> using is_braces_constructible = decltype(test_is_braces_constructible<T, TArgs...>(0)); | |
struct any_type { | |
template<class T> | |
constexpr operator T(); // non explicit | |
}; | |
template<class T> | |
auto to_tuple(T&& object) noexcept { | |
using type = std::decay_t<T>; | |
if constexpr(is_braces_constructible<type, any_type, any_type, any_type, any_type>{}) { | |
auto&& [p1, p2, p3, p4] = object; | |
return std::make_tuple(p1, p2, p3, p4); | |
} else if constexpr(is_braces_constructible<type, any_type, any_type, any_type>{}) { | |
auto&& [p1, p2, p3] = object; | |
return std::make_tuple(p1, p2, p3); | |
} else if constexpr(is_braces_constructible<type, any_type, any_type>{}) { | |
auto&& [p1, p2] = object; | |
return std::make_tuple(p1, p2); | |
} else if constexpr(is_braces_constructible<type, any_type>{}) { | |
auto&& [p1] = object; | |
return std::make_tuple(p1); | |
} else { | |
return std::make_tuple(); | |
} | |
} | |
int main() { | |
{ | |
struct s { | |
int p1; | |
double p2; | |
}; | |
auto t = to_tuple(s{1, 2.0}); | |
static_assert(std::is_same<std::tuple<int, double>, decltype(t)>{}); | |
assert(1 == std::get<0>(t)); | |
assert(2.0 == std::get<1>(t)); | |
} | |
{ | |
struct s { | |
struct nested { } p1; | |
int p2; | |
int p3; | |
s* p4; | |
}; | |
auto t = to_tuple(s{s::nested{}, 42, 87, nullptr}); | |
static_assert(std::is_same<std::tuple<s::nested, int, int, s*>, decltype(t)>{}); | |
assert(42 == std::get<1>(t)); | |
assert(87 == std::get<2>(t)); | |
assert(nullptr == std::get<3>(t)); | |
} | |
} |
This works and is interesting, but what I'm looking for is a way to enumerate the fields in a struct to get their names and types. Your example deals only with the values, which I don't care about for my current use case.
maybe https://www.programmersought.com/article/95015482911/ can work for you
and the code show at
https://github.com/yunpengxiao/yunpengxiao.github.io/blob/master/articles/2018/Cpp-Struct-Field-Reflection/static_reflection.h
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works and is interesting, but what I'm looking for is a way to enumerate the fields in a struct to get their names and types. Your example deals only with the values, which I don't care about for my current use case.