即所有标准库的实现(在MSVC、clang、gcc中)都使用以下代码(为了可读性而简化):
但为什么不使用花括号呢?例如:
(同样的问题也适用于
template<class T, class... Args>
inline unique_ptr<T> make_unique(Args&&... args)
{
return unique_ptr<T>(new T(std::forward<Args>(args)...));
}
但为什么不使用花括号呢?例如:
template<class T, class... Args>
inline unique_ptr<T> make_unique(Args&&... args)
{
return unique_ptr<T>(new T{std::forward<Args>(args)...});
// ^ here and ^ here
}
(同样的问题也适用于
make_shared
。)
std::make_unique<std::vector<int>>({2,3})
。 - aschepler{2, 3}
可以推导出来,否则它将无法编译。 - Jarod42std::vector
时使用类似这样的方法会出现问题,如果你用花括号和圆括号提供两个整数参数,则会有非常不同的结果。 - AndyG