c++ - emplace_back() 如何在 Visual Studio 2012 中没有可变参数模板的情况下工作?

标签 c++ c++11 visual-c++

使用带有工具集 v110 的 Visual Studio 2012 我可以使用容器的 emplace_back()功能但不能使用 std::forward使用可变参数模板 - 接受任意数量的参数。它是怎么做到的?

例如,make_unique<>()不会编译:

template<typename T, typename... Ts>
std::unique_ptr<T> make_unique(Ts&&... params)
{
  return std::unique_ptr<T>(new T(std::forward<Ts>(params)...));
}

最佳答案

根据 this official list在受支持的 C++11 语言功能中,可变参数模板在 VS 2012 中不受支持。

并且在下面的同一页上有如下注释:

Variadics: Visual C++ in Visual Studio 2012 had a scheme for simulating variadic templates. In Visual C++ in Visual Studio 2013, the simulations are gone and variadics are fully implemented. If your code relies on the old simulated variadics behavior, you have to fix it. However, the switch to real variadic templates has improved compile times and reduced compiler memory consumption.

然后 another page有一条说明模拟是如何完成的:

Over the years, we've simulated variadic templates with two different systems of "faux variadic" preprocessor macros - the first system involved repeatedly including subheaders, while the second system (more elegant, as far as crawling horrors go) eliminated the subheaders and replaced them with big backslash-continued macros that were stamped out by other macros. Functions that were supposed to be true variadic, like make_shared(args...), were actually implemented with overloads: make_shared(), make_shared(arg0), make_shared(arg0, arg1), etc. Classes that were supposed to be true variadic, like tuple, were actually implemented with default template arguments and partial specializations. This allowed us to bring you make_shared/tuple/etc. years ago, but it had lots of problems. The macros were very difficult to maintain, making it hard to find and fix bugs in the affected code. Spamming out so many overloads and specializations increased compile times, and degraded Intellisense. Finally, there was the infinity problem. We originally stamped out overloads/specializations for 0 to 10 arguments inclusive, but as the amount of variadic machinery increased from TR1 through C++0x's evolution to C++11's final form, we lowered infinity from 10 to 5 in Visual C++ 2012 in order to improve compile times for most users (we provided a way to request the old limit of 10 through a macro, _VARIADIC_MAX).

关于c++ - emplace_back() 如何在 Visual Studio 2012 中没有可变参数模板的情况下工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30340031/

相关文章:

c++ - 尝试避免某些特定 C++ 代码的代码重复

c++ - 在 C++ 中进行序列化

c++ - 如何在 C++ 中将 Foo** 指针转换为 const Foo**

c++ - atoi(str) 中的段错误

c++ - std::unordered_map 中的可能错误

c++ - move 构造函数和 char 数组参数

C++在tensorflow中使用Eigen

c++ - 下标运算符的标准 C++ 函数对象模板

c++ - Printf 没有打印任何要输出的内容? C++ SDL

c++ - Visual C++ 2010 中的 UTF-8 语言环境