getml / reflect-cpp

A C++20 library for fast serialization, deserialization and validation using reflection. Supports JSON, BSON, CBOR, flexbuffers, msgpack, TOML, XML, YAML / msgpack.org[C++20]
https://getml.github.io/reflect-cpp/
MIT License
821 stars 65 forks source link

no fields type support #80

Closed Cirnoo closed 3 months ago

Cirnoo commented 3 months ago

Hi, can i use refect-cpp with some type has no fields? thanks.

int a = 0;
rfl::to_view(a);

/usr/local/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__tuple/tuple_element.h:86:1: error: implicit instantiation of undefined template 'std::tuple_element<16, int>' 86 | using tuple_element_t _LIBCPP_NODEBUG = typename tuple_element <_Ip, _Tp...>::type; | ^ /Users/luna/CLionProjects/libgc/./reflect-cpp-0.8.0/include/rfl/parsing/../internal/has_fields.hpp:37:40: note: in instantiation of template type alias 'tuple_element_t' requested here 37 | using T = std::remove_cvref_t<std::tuple_element_t<_i, TupleType>>; | ^ /Users/luna/CLionProjects/libgc/./reflect-cpp-0.8.0/include/rfl/parsing/../internal/has_fields.hpp:43:31: note: in instantiation of function template specialization 'rfl::internal::some_fields_or_flatten<int, 16>' requested here 43 | return is_field_v || some_fields_or_flatten<TupleType, _i + 1>(); | ^ /Users/luna/CLionProjects/libgc/./reflect-cpp-0.8.0/include/rfl/parsing/../internal/has_fields.hpp:43:31: note: in instantiation of function template specialization 'rfl::internal::some_fields_or_flatten<int, 15>' requested here /Users/luna/CLionProjects/libgc/./reflect-cpp-0.8.0/include/rfl/parsing/../internal/has_fields.hpp:43:31: note: in instantiation of function template specialization 'rfl::internal::some_fields_or_flatten<int, 14>' requested here /Users/luna/CLionProjects/libgc/./reflect-cpp-0.8.0/include/rfl/parsing/../internal/has_fields.hpp:43:31: note: in instantiation of function template specialization 'rfl::internal::some_fields_or_flatten<int, 13>' requested here /Users/luna/CLionProjects/libgc/./reflect-cpp-0.8.0/include/rfl/parsing/../internal/has_fields.hpp:43:31: note: (skipping 11 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all) /Users/luna/CLionProjects/libgc/./reflect-cpp-0.8.0/include/rfl/parsing/../internal/has_fields.hpp:43:31: note: in instantiation of function template specialization 'rfl::internal::some_fields_or_flatten<int, 1>' requested here /Users/luna/CLionProjects/libgc/./reflect-cpp-0.8.0/include/rfl/parsing/../internal/has_fields.hpp:54:19: note: in instantiation of function template specialization 'rfl::internal::some_fields_or_flatten<int, 0>' requested here 54 | if constexpr (some_fields_or_flatten()) { | ^ /Users/luna/CLionProjects/libgc/./reflect-cpp-0.8.0/include/rfl/parsing/../internal/../internal/to_ptr_named_tuple.hpp:57:17: note: in instantiation of function template specialization 'rfl::internal::has_fields' requested here 57 | if constexpr (has_fields<std::remove_cvref_t>()) { | ^ /Users/luna/CLionProjects/libgc/./reflect-cpp-0.8.0/include/rfl/parsing/../to_view.hpp:14:20: note: in instantiation of function template specialization 'rfl::internal::to_ptr_named_tuple<int &>' requested here 14 | return internal::to_ptr_named_tuple(_t); | ^ /Users/luna/CLionProjects/libgc/main.cpp:59:10: note: in instantiation of function template specialization 'rfl::to_view' requested here 59 | rfl::to_view(a); | ^ /usr/local/Cellar/llvm/17.0.6_1/bin/../include/c++/v1/__tuple/tuple_element.h:26:62: note: template is declared here 26 | template <size_t _Ip, class _Tp> struct _LIBCPP_TEMPLATE_VIS tuple_element;

liuzicheng1987 commented 3 months ago

Creating a view on an int doesn't make a ton of sense...the idea of a view is to return the field names and pointers to the fields in a way that can be conveniently handled at compile time. But if there are no fields to begin with, there isn't a lot to be done. Just use a pointer instead.

That doesn't mean that reflect-cpp doesn't support plain ints. For instance, rfl::json::write(a) will absolutely work.