我正想通性病的自我学习:: make_unique功能,我发现下面的声明在cppreference.com在C新型功能++ - 这是什么代码意味着
template< class T, class... Args >
unique_ptr<T> make_unique(Args&&... args);
我完全无法理解的签名上面的方法/功能。
我正想通性病的自我学习:: make_unique功能,我发现下面的声明在cppreference.com在C新型功能++ - 这是什么代码意味着
template< class T, class... Args >
unique_ptr<T> make_unique(Args&&... args);
我完全无法理解的签名上面的方法/功能。
有在此声明中使用了许多 “新” 的特点:
template
关键字;见https://en.wikipedia.org/wiki/Template_(C%2B%2B))...
参数;见https://en.wikipedia.org/wiki/Variadic_template)unique_ptr
,参见https://en.wikipedia.org/wiki/Smart_pointer)Args&&
,参见https://en.wikipedia.org/wiki/C%2B%2B11#Rvalue_references_and_move_constructors)基本上,代码的意思是“为任意类型的任意数量的参数声明一个函数模板并返回专用于给定类型T的unique_ptr”。另外,右值引用(& &)告诉您参数将被移动而不是被复制。
总之:make_unique<Type>(v)
基本上与unique_ptr<Type>(new Type(v))
相同。
除了既方便又相当于C++ 11的std :: make_shared
你的第二个问题使得这个问题太广泛了。你为什么试图学习每一个新功能? – Tas
仅供参考:https://isocpp.org/files/papers/p0636r0.html – Tas
否 - 并非所有,但至少他们是如何学习C++ 98同样我希望逐步提升我的技能到新的水平通过练习的一步练习,就像我学习C++ 98 – Prakash