【发布时间】:2021-11-04 14:41:07
【问题描述】:
我有一个模板类型参数跟随参数包的函数。它看起来像这样:
template<typename...Args, typename T>
T* default_factory_func()
{
return new T;
}
Visual C++ 编译器以错误 C3547: template parameter 'T' cannot be used because it follows a template parameter pack and cannot be deduced from the function parameters of 'default_factory_func' 拒绝它。
但是,我尝试了 Compiler Explorer 上可用的各种版本的 GCC(从 4.4.7 开始)和 clang(从 3.1 开始),它们都可以很好地编译此类代码。
// this code is just a minimal example condensed
// from a much more complex codebase
template<typename T>
T* construct(T* (*factory_func)())
{
return factory_func();
}
template<typename...Args, typename T>
T* default_factory_func() // C3547 on this line
{
return new T(Args()...);
}
struct some_class {
some_class(int, int, int) {}
};
int main()
{
construct<some_class>(
default_factory_func<int,int,int>
);
}
这是 MSVC 的一些怪癖还是标准不允许?
【问题讨论】:
标签: c++ visual-c++ variadic-templates