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

Constructor broken with gcc-4.8.5 #75

Open
rnburn opened this issue Apr 21, 2020 · 1 comment
Open

Constructor broken with gcc-4.8.5 #75

rnburn opened this issue Apr 21, 2020 · 1 comment

Comments

@rnburn
Copy link

rnburn commented Apr 21, 2020

#include <iostream>

#include "variant.hpp"

struct sv {
        sv(const char*) {}
};

int main() {
        mpark::variant<bool, sv> v{"abc"};
        std::cout << v.index() << "\n";
        return 0;
}

With gcc-4.8.5, this code incorrectly prints 0.

It works for later versions of gcc (tested 7.5.0) and prints 1.

I tested against master 3c7fc82

@rnburn
Copy link
Author

rnburn commented May 1, 2020

It looks like the bool conversion logic in overload_leaf is disabled for gcc < 5.0 (I'm guessing because the narrowing conversion check doesn't work).

    template <typename Arg, std::size_t I, typename T>
    struct overload_leaf<
        Arg,
        I,
        T,
        true
#if defined(__clang__) || !defined(__GNUC__) || __GNUC__ >= 5
        ,
        lib::enable_if_t<
            std::is_same<lib::remove_cvref_t<T>, bool>::value
                ? std::is_same<lib::remove_cvref_t<Arg>, bool>::value
                : is_non_narrowing_convertible<Arg, T>::value>
#endif
        > {
      using impl = lib::size_constant<I> (*)(T);
      operator impl() const { return nullptr; };
    };

But could this be changed to

template <typename Arg, std::size_t I, typename T>
struct overload_leaf<Arg,
                     I,
                     T,
                     true,
                     enable_if_t<std::is_same<remove_cvref_t<T>, bool>::value
                                     ? std::is_same<remove_cvref_t<Arg>, bool>::value
                                     :
#if defined(__clang__) || !defined(__GNUC__) || __GNUC__ >= 5
                                     is_non_narrowing_convertible<Arg, T>::value
#else
                                     std::is_convertible<Arg, T>::value
#endif
                                 >>
{
  using impl = size_constant<I> (*)(T);
  operator impl() const { return nullptr; };
};

that would be closer to the correct std::variant behavior

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

1 participant