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

store size_t in a variant ? #40

Closed
SuperFola opened this issue Feb 4, 2018 · 7 comments
Closed

store size_t in a variant ? #40

SuperFola opened this issue Feb 4, 2018 · 7 comments

Comments

@SuperFola
Copy link

SuperFola commented Feb 4, 2018

Hi,

I am trying to store size_t as well as int64_t, double, bool, string in a variant. Storing the 4 last types work fine, but I get a bad_variant_access when trying to get/set the size_t with this code :

#include <iostream>
#include <cstdint>
#define MPARK_EXCEPTIONS
#include <mpark/variant.hpp>

using namespace std;  // i know it's bad, it's just for testing

int main()
{
    mpark::variant<int64_t, double, bool, string, size_t> v;
    mpakr::get<size_t>(v) = 15;  // right there
    return 0;
}

edit : updating code to fix compile errors

@SaadAttieh
Copy link

SaadAttieh commented Feb 4, 2018 via email

@SuperFola
Copy link
Author

Even this way, I am still getting a bad_variant_access :/

@mpark
Copy link
Owner

mpark commented Feb 5, 2018

@SuperFola: What configuration are you using? @SaadAttieh's snippet works fine for me:
https://wandbox.org/permlink/GFYehONmvuc7cByX

@SuperFola
Copy link
Author

I am using gnu gcc (mingw 4.8), with -Wall -std=c++14 -g on Windows (I want to add that I had to add the define MPARK_EXCEPTIONS to be able to see the error, otherwise I would just get a terminate without an active exception).

@mpark
Copy link
Owner

mpark commented Feb 10, 2018

@SuperFola: Does this issue only arise with size_t in specific..? or are you able to reproduce it with any type?

@SuperFola
Copy link
Author

SuperFola commented Feb 12, 2018

Using size_t or uint32_t seems to be a problem, I can try others things (it seems to be a problem only with unsigned integers type) if you want :)

edit :

#include <iostream>
#include <cstdint>

#define MPARK_EXCEPTIONS
#include "variant.hpp"

using namespace std;

int main()
{
    mpark::variant<
        int8_t,  int16_t,  int32_t,  int64_t, uint8_t, uint16_t, /*uint32_t,*/ uint64_t, size_t
    > v;

    cout << 1 << flush; mpark::get<int8_t>(v)   = 15;
    cout << 2 << flush; mpark::get<int16_t>(v)  = 15;
    cout << 3 << flush; mpark::get<int32_t>(v)  = 15;
    cout << 4 << flush; mpark::get<int64_t>(v)  = 15;
    cout << 5 << flush; mpark::get<uint8_t>(v)  = 15;
    cout << 6 << flush; mpark::get<uint16_t>(v) = 15;
    // if I add this line, it refuses to compile
    // cout << 7 << flush; mpark::get<uint32_t>(v) = 15;
    cout << 8 << flush; mpark::get<uint64_t>(v) = 15;
    // same here as long as uint32_t is in the variant
    cout << 9 << flush; mpark::get<size_t>(v)   = 15;

    return 0;
}

this code compiles but is throwing a bad variant access

12terminate called after throwing an instance of 'mpark::bad_variant_access'
  what():  bad_variant_access

I tried to comment some of the "tests" and it seems to be a problem to have different integers type in the variant :

    mpark::variant<
        int8_t,  int16_t,  int32_t,  int64_t, uint8_t, uint16_t, /*uint32_t,*/ uint64_t /*size_t*/
    > v;

    cout << 1 << flush; mpark::get<int8_t>(v)   = 15;
    // cout << 2 << flush; mpark::get<int16_t>(v)  = 15;
    // cout << 3 << flush; mpark::get<int32_t>(v)  = 15;
    cout << 4 << flush; mpark::get<int64_t>(v)  = 15;
    cout << 5 << flush; mpark::get<uint8_t>(v)  = 15;
    cout << 6 << flush; mpark::get<uint16_t>(v) = 15;
    // if I add this line, it refuses to compile
    // cout << 7 << flush; mpark::get<uint32_t>(v) = 15;
    cout << 8 << flush; mpark::get<uint64_t>(v) = 15;
    // same here as long as uint32_t is in the variant
    //cout << 9 << flush; mpark::get<size_t>(v)   = 15;

    return 0;

still throws a bad_variant_access :/

@mpark
Copy link
Owner

mpark commented Jan 14, 2019

I'm going to close this for now since MinGW is not a distro that I'm officially supporting at this point. If there are enough requests for MinGW support going forward, I'll perhaps invest more time.

@mpark mpark closed this as completed Jan 14, 2019
@mpark mpark reopened this Jan 15, 2019
@mpark mpark closed this as completed Jan 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants