Skip to content
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

cereal::binary_data() with lvalue #396

Closed
albertziegenhagel opened this issue Apr 11, 2017 · 1 comment
Closed

cereal::binary_data() with lvalue #396

albertziegenhagel opened this issue Apr 11, 2017 · 1 comment
Labels
Milestone

Comments

@albertziegenhagel
Copy link

If you pass an lvalue pointer to a const type to cereal::binary_data() the code does not compile with:

include\cereal/details/helpers.hpp(217): error C2440: 'initializing': cannot convert from 'const int *' to 'void *'

Here is a short code to reproduce the issue:

#include <fstream>

#include <cereal/cereal.hpp>
#include <cereal/archives/binary.hpp>

struct MyVector
{
public:
    const int* data() const { return data_; }
private:
    int data_[3];
};

template<typename Archive>
void save(Archive& ar, const MyVector& v)
{
    auto local_data = v.data();
    ar(cereal::binary_data(local_data, 3)); // fails
    //ar(cereal::binary_data(v.data(), 3)); // works
}

int main(int argc, char* argv[])
{
    std::ofstream file("test.dat", std::fstream::binary);

    cereal::BinaryOutputArchive archive(file);

    MyVector v;
    archive(v);
}

The reason for this is that cereal::binary_data() uses a "universal reference" as first parameter which is forwarded as const int*& in the example above which is afterwards not handled correctly in struct BinaryData

@AzothAmmo
Copy link
Contributor

Good catch, I've merged your PR to develop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants