C3520参数包必须扩展 - 使用' variadic的行为不正确。

发布于 2025-01-19 06:13:54 字数 512 浏览 1 评论 0原文

在使用Microsoft Visual C ++编译器15.9.28307.1300(AMD64)和C ++ 17标准I标准i使用Microsoft Visual C ++编译器使用QT 5.12编译下面的代码时

错误c3520:'args':必须在此上下文中扩展参数包 注意:请参阅类模板实例化的参考 'helper< args ...>'被编译

template<typename T> 
struct Base { 
    void operator()(const T& arg){} 
}; 

template <typename... Args>
class Helper : Base<Args>... {
public:
    using Base<Args>::operator()...;
};

是MSVC的错误吗?有什么已知的解决方法吗?相同的代码使用clang在macOS上编译。

While compiling the code below with Qt 5.12 using Microsoft Visual C++ Compiler 15.9.28307.1300 (amd64) and c++17 standard I get the following error:

error C3520: 'Args': parameter pack must be expanded in this context
note: see reference to class template instantiation
'Helper<Args...>' being compiled

template<typename T> 
struct Base { 
    void operator()(const T& arg){} 
}; 

template <typename... Args>
class Helper : Base<Args>... {
public:
    using Base<Args>::operator()...;
};

Is this a bug with msvc? Is there any known workaround? The same code compiles on macOS using clang.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残龙傲雪 2025-01-26 06:13:54

作为可变参数 using (C++17) 的解决方法,您可以使用递归方式:

template <typename... Args>
class Helper;

template <>
class Helper<>
{
};

template <typename T>
class Helper<T> : Base<T>
{
public:
    using Base<T>::operator();
};

template <typename T, typename... Ts>
class Helper<T, Ts...> : Base<T>, Helper<Ts...>
{
public:
    using Base<T>::operator();
    using Helper<Ts...>::operator();
};

演示

As workaround to variadic using (C++17), you might use the recursive way:

template <typename... Args>
class Helper;

template <>
class Helper<>
{
};

template <typename T>
class Helper<T> : Base<T>
{
public:
    using Base<T>::operator();
};

template <typename T, typename... Ts>
class Helper<T, Ts...> : Base<T>, Helper<Ts...>
{
public:
    using Base<T>::operator();
    using Helper<Ts...>::operator();
};

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文