Variadic base class using declaration fails to compile in MSVC

夙愿已清 提交于 2020-02-04 20:57:19

问题


I'm trying to implement a variadic visitor class.

template<typename T>
class VisitorBaseFor {
protected:
   virtual ~VisitorBaseFor() = default;

public:
   virtual void visit(T &t) = 0;
};

template<typename... Ts>
class VisitorBase : public VisitorBaseFor<Ts>... {
public:
    using VisitorBaseFor<Ts>::visit...;
};

I know from that overload trick that variadic using declarations should be possible, but MSVC does not compile my code saying I need to expand Ts while both GCC and Clang compile my code without errors, see here.

What am I missing? Is this a MSVC bug or just not (yet) supported? If it is, is there a way to work around this?

Apart from that I have tried to remove the using declaration but then the calls to visit become ambiguous for some reason, even though all classes in Ts are not convertible to each other. This is being diagnosed correctly by MSVC, but why are they even used in overload resolution then?

Update: This is a known bug since at least Sep 03, 2018. See here and here.


回答1:


Code is indeed correct and so bug of msvc.

Workaround is to manually do the recursion:

template<typename T>
class VisitorBaseImpl {
protected:
    virtual ~VisitorBaseImpl() = default;

public:
    virtual void visit(T &t) = 0;
};

template<typename... Ts> class VisitorBase;

template<typename T>
class VisitorBase<T> : public VisitorBaseImpl<T>
{
};

template<typename T, typename... Ts>
class VisitorBase<T, Ts...> : public VisitorBase<T>, public VisitorBase<Ts...>
{
public:
    using VisitorBase<T>::visit;
    using VisitorBase<Ts...>::visit;
};

Demo



来源:https://stackoverflow.com/questions/57554389/variadic-base-class-using-declaration-fails-to-compile-in-msvc

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