Aliasing boost::variant or std::variant not compilable

好久不见. 提交于 2019-12-23 00:42: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.



来源:https://stackoverflow.com/questions/48360986/aliasing-boostvariant-or-stdvariant-not-compilable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!