-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem serializing std::tuple #22
Comments
It looks like the problem is with it computing the wrong aggregate arity for tuple. Adding the following specialization seems to fix it (although note that there is another problem that needs to be addressed for serialization to work on g++11). template<typename ...Ts>
struct aggregate_arity<std::tuple<Ts...>> : std::make_index_sequence<sizeof...(Ts)> {}; |
struct MyType {
std::tuple<int, double> t(1, 2.3);
}; |
Thanks for the quick response. Unfortunately, same problem if I write it with #include <tuple>
#include <vector>
#include <cstdint>
std::vector<uint8_t> bytes;
struct MyType {
std::tuple<int, double> t{1, 2.3};
};
MyType m;
#include <alpaca/alpaca.h>
auto bytes_written = alpaca::serialize(m.t, bytes); which again produces
But this works with #include <alpaca/alpaca.h>
namespace alpaca::detail {
template<typename ...Ts>
struct aggregate_arity<std::tuple<Ts...>> : std::make_index_sequence<sizeof...(Ts)> {};
}
#include <tuple>
#include <vector>
#include <cstdint>
std::vector<uint8_t> bytes;
struct MyType {
std::tuple<int, double> t{1, 2.3};
};
MyType m;
auto bytes_written = alpaca::serialize(m.t, bytes); |
The serialize function is expecting the struct object, not each field. If you pass in the field itself, then wrapping it in a struct does nothing. This should work: #include <tuple>
#include <vector>
#include <cstdint>
std::vector<uint8_t> bytes;
struct MyType {
std::tuple<int, double> t{1, 2.3};
};
MyType m;
#include <alpaca/alpaca.h>
auto bytes_written = alpaca::serialize(m, bytes); |
Of course. Yes, that seems to work. It also seems to resolve my other issue as well. Thanks very much! Granting all of that, it looks like the code changes I described make top-level tuples work. Do you think that would be worth enabling? (May overlap with this issue) |
As long as there are no regressions (unit tests), I'd be open to it, yes :) Feel free to create a PR! |
It looks like I spoke too soon about it working if you wrap a tuple in a struct. If you modify your example to have only one type in the struct, serializing produces 0 bytes; #include <tuple>
#include <vector>
#include <cstdint>
std::vector<uint8_t> bytes;
struct MyType {
std::tuple<int> t{1};
};
MyType m;
#include <alpaca/alpaca.h>
auto bytes_written = alpaca::serialize(m, bytes); // bytes_written is 0 The reason for this is that the conversion from I'll try to create a PR that fixes all tuple-related issues. |
I am not able to get tuples to serialize using g++ 11.3.0 on ubuntu 22.04.
Here's my program
which fails to compile as follows
Any thoughts appreciated.
Thanks,
Mike
The text was updated successfully, but these errors were encountered: