使用声明的可变参数基类无法在MSVC中编译 [英] Variadic base class using declaration fails to compile in MSVC

查看:108
本文介绍了使用声明的可变参数基类无法在MSVC中编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现可变的访问者类。

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...;
};

我从那种重载技巧应该可以使用可变的声明,但是MSVC不会编译我的代码,说我需要在GCC和Clang都编译我的代码时扩展Ts没有错误,请参见此处

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.

我是什么失踪?这是MSVC错误还是尚不支持?

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?

除此之外,我尝试删除了using声明,但是由于某些原因,访问调用变得模棱两可,即使所有T中的类不能相互转换。 MSVC可以正确诊断出该错误,但是为什么还要在过载解析中使用它们?

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?

更新:自2018年9月3日以来,这是一个已知的错误。请参见此处此处

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

推荐答案

代码确实正确,因此存在msvc的错误。

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;
};

演示

这篇关于使用声明的可变参数基类无法在MSVC中编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆