Aliasing boost::variant or std::variant not compil

2019-05-29 15:23发布

问题:

Currently my library uses boost::optional and boost::variant. Since C++17 is out, I would like to add an option, that it works with the boost and std.

So I tested the complete code with boost optional and variant and std optional and variant successful.

So I added a header files that is similar to this:

#ifdef USE_BOOST
#include <boost/optional.hpp>
#elif USE_STD
#include <optional>
#endif

namespace Foo {

#ifdef USE_BOOST
template <typename T>
using optional = boost::optional<T>
#elif USE_STD
template <typename T>
using optional = std::optional<T>
#endif

}

And everything works stil fine.

Then I added something similar for variant

#ifdef USE_BOOST
#include <boost/variant.hpp>
#elif USE_STD
#include <variant>
#endif

namespace Foo {

#ifdef USE_BOOST
template <typename... T>
using variant = boost::variant<T...>
// similar to the following
#elif USE_STD

template <typename...T>
using variant = std::variant<T...>;

template <class T, class... Types>
constexpr T& get(std::variant<Types...>& v) {
    return std::get<T>(v);
};

template <class T, class... Types>
constexpr T&& get(std::variant<Types...>&& v) {
    return std::get<T>(std::move(v));
};

template <class T, class... Types>
constexpr const T& get(const std::variant<Types...>& v) {
    return std::get<T>(v);
};

template <class T, class... Types>
constexpr const T&& get(const std::variant<Types...>&& v) {
    return std::get<T>(std::move(v));
};

#endif

}

And within the library I use now everywhere Foo::optional and Foo::variant. But now all function templates like

template <typename... Args>
bool bar(const std::tuple<variant<Args,
                                  std::exception_ptr>...>& args)

are not found any more. (clang error message: no matching function for call to ...)

Tested with clang trunk and VS 2017 15.6.1 Preview Any idea?

Many thanks in advance!

Edit: A minimal example is in my gist

回答1:

Finally problem solved. It is a bug in clang an VS. GCC compiles it fine. The solution for clang and VS is calling the function with explicit template parameters.



标签: c++ boost c++17