如何在Visual Studio 2017中支持可变参数模板类型 [英] How to Support Variadic Template Types in Visual Studio 2017

查看:345
本文介绍了如何在Visual Studio 2017中支持可变参数模板类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

T.C.在这里给出了一个巧妙的解决方案,该解决方案使用了此对象:

T.C. gave a clever solution here which used this object:

template<class... Ts> struct overload : Ts... { using Ts::operator()...; };
template<class... Ts> overload(Ts...) -> overload<Ts...>;
struct fallback_t { template<class T> fallback_t(T&&) {} };

不幸的是,我无法在Visual Studio 2017中进行编译.出现错误:

Unfortunately I can't get that to compile in Visual Studio 2017. I get the errors:

1>警告C4346:'Ts::()':相关名称不是类型
1>注意:带'typename'的前缀表示类型
1>注意:请参见对正在编译的类模板实例化"owner :: overload"的引用
1>错误C2143:语法错误:缺少';'在"..."之前
1>错误C2059:语法错误:'...'
1>错误C2238:;"之前的意外令牌
1>错误C2988:无法识别的模板声明/定义
1>错误C2143:语法错误:在'...'之前缺少')'
1>错误C2143:语法错误:缺少';'在"..."之前
1>错误C2365:"Ts":重新定义;先前的定义是模板参数"
1>注意:请参见"Ts"的声明
1>错误C2238:;"之前的意外令牌
1>错误C2059:语法错误:'...'
1>错误C2059:语法错误:')'
1>错误C2955:'owner :: overload':使用类模板需要模板参数列表
1>注意:请参见'owner :: overload'的声明
1>错误C2664:'无效的owner :: bar ::
:: operator()(owner :: fallback_t)const':无法将参数1从'owner :: fallback_t'转换为'owner :: fallback_t'
1>注意:使用未定义类型'owner :: fallback_t'
1>注意:请参见'owner :: fallback_t'的声明
1>错误C2672:'owner :: findObject':找不到匹配的重载函数
1>错误C2780:无效所有者:: findObject(int,const T&)":需要2个参数-提供了1个
1>注意:请参见'owner :: findObject'的声明

1> warning C4346: 'Ts::()': dependent name is not a type
1> note: prefix with 'typename' to indicate a type
1> note: see reference to class template instantiation 'owner::overload' being compiled
1> error C2143: syntax error: missing ';' before '...'
1> error C2059: syntax error: '...'
1> error C2238: unexpected token(s) preceding ';'
1> error C2988: unrecognizable template declaration/definition
1> error C2143: syntax error: missing ')' before '...'
1> error C2143: syntax error: missing ';' before '...'
1> error C2365: 'Ts': redefinition; previous definition was 'template parameter'
1> note: see declaration of 'Ts'
1> error C2238: unexpected token(s) preceding ';'
1> error C2059: syntax error: '...'
1> error C2059: syntax error: ')'
1> error C2955: 'owner::overload': use of class template requires template argument list
1> note: see declaration of 'owner::overload'
1> error C2664: 'void owner::bar::
::operator ()(owner::fallback_t) const': cannot convert argument 1 from 'owner::fallback_t' to 'owner::fallback_t'
1> note: use of undefined type 'owner::fallback_t'
1> note: see declaration of 'owner::fallback_t'
1> error C2672: 'owner::findObject': no matching overloaded function found
1> error C2780: 'void owner::findObject(int,const T &)': expects 2 arguments - 1 provided
1> note: see declaration of 'owner::findObject'

我可以采取什么手段使Visual Studio尊重可变参数模板吗?

Is there any trickery that I can do to make Visual Studio respect the variadic template in?

推荐答案

您可以尝试在

You can try to compile in c++17 mode with /std::c++17 as @S.M. has noted; note that some C++17 features can break existing code. In addition as @milesbudnek has noted, as of today MSVC doesn't support the requried features.

如果您不能使用C ++ 17模式,则可以在.

If you cannot use C++17 mode you can implement this in c++14 in this SO answer.

我要进行一些更改:

template <class... Fs>
struct overload_t;

// zero
template<>
struct overload_t<> {};

// >1
template <class F0, class... Frest>
struct overload_t<F0, Frest...> :
  F0,
  overload_t<Frest...>
{
    overload_t(F0 f0, Frest... rest) :
      F0(std::move(f0)), overload_t<Frest...>(std::move(rest)...)
   {}

    using F0::operator();
    using overload_t<Frest...>::operator();
};

// 1
template <class F0>
struct overload_t<F0> : F0
{
    overload_t(F0 f0) : F0(std::move(f0)) {}

    using F0::operator();
};

template <class... Fs>
auto overload(Fs... fs)
{
  return overload_t<Fs...>(std::move(fs)...);
}

这篇关于如何在Visual Studio 2017中支持可变参数模板类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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