From a5a79a594f39d705a7ef969f54a0743516f0bc6d Mon Sep 17 00:00:00 2001 From: artemp Date: Mon, 24 Oct 2016 10:59:30 +0200 Subject: [PATCH] Fix #122 by adding an extra compile check in universal ctor (via @lightmare) + test case --- Makefile | 2 +- include/mapbox/variant.hpp | 2 +- test/t/issue122.cpp | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 test/t/issue122.cpp diff --git a/Makefile b/Makefile index b20f9ee..fc554b8 100644 --- a/Makefile +++ b/Makefile @@ -61,7 +61,7 @@ out/%.o: test/t/%.cpp Makefile $(ALL_HEADERS) mkdir -p ./out $(CXX) -c -o $@ $< -Iinclude -Itest/include $(DEBUG_FLAGS) $(COMMON_FLAGS) $(CXXFLAGS) -out/unit: out/unit.o out/binary_visitor_1.o out/binary_visitor_2.o out/binary_visitor_3.o out/binary_visitor_4.o out/binary_visitor_5.o out/binary_visitor_6.o out/issue21.o out/mutating_visitor.o out/optional.o out/recursive_wrapper.o out/sizeof.o out/unary_visitor.o out/variant.o +out/unit: out/unit.o out/binary_visitor_1.o out/binary_visitor_2.o out/binary_visitor_3.o out/binary_visitor_4.o out/binary_visitor_5.o out/binary_visitor_6.o out/issue21.o out/issue122.o out/mutating_visitor.o out/optional.o out/recursive_wrapper.o out/sizeof.o out/unary_visitor.o out/variant.o mkdir -p ./out $(CXX) -o $@ $^ $(LDFLAGS) diff --git a/include/mapbox/variant.hpp b/include/mapbox/variant.hpp index 993db16..afdf36a 100644 --- a/include/mapbox/variant.hpp +++ b/include/mapbox/variant.hpp @@ -570,7 +570,7 @@ class variant // http://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers template , - typename Enable = typename std::enable_if::type> + typename Enable = typename std::enable_if, typename Traits::value_type>::value>::type > VARIANT_INLINE variant(T&& val) noexcept(std::is_nothrow_constructible::value) : type_index(Traits::index) { diff --git a/test/t/issue122.cpp b/test/t/issue122.cpp new file mode 100644 index 0000000..12de7d5 --- /dev/null +++ b/test/t/issue122.cpp @@ -0,0 +1,20 @@ +#include "catch.hpp" + +#include +#include + +// https://github.com/mapbox/variant/issues/122 + +struct X +{ + template + X(const ValueType&) {} +}; + + +TEST_CASE("Correctly choose appropriate constructor", "[variant]") +{ + mapbox::util::variant a{123}; + decltype(a) b(a); + REQUIRE(a.which() == b.which()); +}