【发布时间】:2015-09-22 18:59:12
【问题描述】:
STL 中有一些以make_ 前缀开头的函数,如std::make_pair、std::make_shared、std::make_unique 等。为什么使用它们而不是简单地使用构造函数更好?
auto pair2 = std::pair< int, double >( 1, 2.0 );
auto pair3 = std::make_pair( 1, 2.0 );
std::shared_ptr< int > pointer1 = std::shared_ptr< int >( new int( 10 ) );
std::shared_ptr< int > pointer2 = std::make_shared< int >( 10 );
- 我只是看到这些函数使代码更短,但仅此而已吗?
- 还有其他优势吗?
- 这些功能使用起来更安全吗?
【问题讨论】:
-
对新的智能指针不太确定,但请查看 stackoverflow.com/questions/9270563/purpose-of-stdmake-pair 的配对。
-
我相信每个都有明显的优势,在单独的现有问题中回答。
make_pairworks without the author knowing the types 参与。make_sharedprovides exception safety. -
少打字会更好?
-
现在我想知道 STL 中有多少
make_函数...