Visual Studio 缺乏对自动模板参数的支持是否有解决方法?
Is There a Workaround for Visual Studio's Lack of Support for auto Template Parameters?
我了解了 c++17'sauto
template parameters in the answer to . A coworker had informed me that they were supported on visual-studio-2017,但我在尝试利用此功能方面似乎不太成功。我写了这个玩具示例来演示我的问题:
struct Foo {
int mem;
};
template <auto T>
decltype(T(Foo{})) bar(const Foo& param)
{
return T(param);
}
int func(const Foo& param) { return param.mem; }
int main() {
Foo myFoo{ 13 };
cout << bar<&func>(myFoo);
}
我相信这是很好的代码,因为它 works fine on gcc 在 Visual Studio 但是我得到了这个:
error C3533: a parameter cannot have a type that contains auto
我已确保我的 "C++ Language Standard" 设置为:"ISO C++ Latest Draft Standard (/std:c++latest)" 但这似乎无法解决问题。 Visual Studio 将支持 auto
之前的模板参数代码,这需要我将函数类型与函数一起作为模板参数传递:template <typename R, R(*T)(const Foo&)> R bar(const Foo& param)
但这与 [= 的优雅不符12=]模板参数。
有没有一种方法可以帮助 Visual Studio 编译 auto
模板代码,或者在 visual-studio-2017 上编译时管理类似的优雅?
这 MS help page 指出:
A method or template parameter cannot be declared with the auto keyword if the default /Zc:auto compiler option is in effect.
所以您要么关闭 /Zc:auto,要么将此函数作为参数传递:
#include <iostream>
struct Foo {
int mem;
};
int func(const Foo& param) {
return param.mem;
}
template <class Func>
auto bar(const Foo& param, Func&& f) {
return f(param);
}
int main() {
Foo f { 42 };
std::cout << bar(f, func);
}
auto
非类型模板参数仅在 VS 2017 版本 15.7.0 中引入。
Templates that are designed to take any type as a non-type parameter can now use the auto
keyword in the template parameter list. This allows instantiations to use any type instead of needing to determine and supply the type of template parameter at the point of instantiation.
不支持该版本之前的版本。
我了解了 c++17'sauto
template parameters in the answer to
struct Foo {
int mem;
};
template <auto T>
decltype(T(Foo{})) bar(const Foo& param)
{
return T(param);
}
int func(const Foo& param) { return param.mem; }
int main() {
Foo myFoo{ 13 };
cout << bar<&func>(myFoo);
}
我相信这是很好的代码,因为它 works fine on gcc 在 Visual Studio 但是我得到了这个:
error C3533: a parameter cannot have a type that contains
auto
我已确保我的 "C++ Language Standard" 设置为:"ISO C++ Latest Draft Standard (/std:c++latest)" 但这似乎无法解决问题。 Visual Studio 将支持 auto
之前的模板参数代码,这需要我将函数类型与函数一起作为模板参数传递:template <typename R, R(*T)(const Foo&)> R bar(const Foo& param)
但这与 [= 的优雅不符12=]模板参数。
有没有一种方法可以帮助 Visual Studio 编译 auto
模板代码,或者在 visual-studio-2017 上编译时管理类似的优雅?
这 MS help page 指出:
A method or template parameter cannot be declared with the auto keyword if the default /Zc:auto compiler option is in effect.
所以您要么关闭 /Zc:auto,要么将此函数作为参数传递:
#include <iostream>
struct Foo {
int mem;
};
int func(const Foo& param) {
return param.mem;
}
template <class Func>
auto bar(const Foo& param, Func&& f) {
return f(param);
}
int main() {
Foo f { 42 };
std::cout << bar(f, func);
}
auto
非类型模板参数仅在 VS 2017 版本 15.7.0 中引入。
Templates that are designed to take any type as a non-type parameter can now use the
auto
keyword in the template parameter list. This allows instantiations to use any type instead of needing to determine and supply the type of template parameter at the point of instantiation.
不支持该版本之前的版本。