使用std :: vector< std :: shared_ptr< const T> >反模式? [英] Is using std::vector< std::shared_ptr<const T> > an antipattern?
问题描述
很久以来,我一直使用 std :: vector
和 std :: shared_ptr
。最近,当需要指向const对象的指针时,我开始使用 std :: shared_ptr< const T>
。这一切OK,因为 std :: shared_ptr< T>
可以转换为 std :: shared_ptr< const T>
但是当我尝试使用 std :: vector< std :: shared_ptr< const T> >
我遇到麻烦。为了简化我将表示两个结构:
template< class T&
using SharedPtrVector = std :: vector< std :: shared_ptr< T> > ;;
template< class T>
使用SharedConstPtrVector = std :: vector< std :: shared_ptr< const T> > ;;问题是,虽然 SharedPtrVector
和
$ b SharedConstPtrVector
非常相似, SharedConstPtrVector
不能转换为 SharedPtrVector
。
所以每次我想成为一个const正确并写一个函数,如:
void f(const SharedConstPtrVector< T>& vec);
没有办法我可以通过const SharedPtrVector< T>
到 f
。
我在想这个很多, >
-
写转换函数
< typename T>
SharedConstPtrVector< T> toConst(const SharedPtrVector< T>&);
-
以通用形式写入代码:
template< typename T>
void f(const std :: vector< std :: shared_ptr< T>>& vec);
或
template< typename TIterator>
void f(TIterator begin,TIterator end);
-
放弃
std :: vector& std :: shared_ptr< const T> >
1.的问题是计算开销和增加的代码丑陋,而2.给出代码一切都是模板的味道。
我是一个没有经验的程序员,我不想出错的方向。
我建议您检查您的设计,以便查看建立那些对象的明确所有者。这是没有明确的所有权,导致人们使用共享的智能指针。
Bjarne Stroustrup建议仅使用智能指针作为最后的手段。他的建议(最好到最差)是:
- 按值存储对象。
Write conversion functions
template <typename T> SharedConstPtrVector<T> toConst(const SharedPtrVector<T>&);
Write code in generic form:
template <typename T> void f(const std::vector< std::shared_ptr<T> >& vec);
or
template <typename TIterator> void f(TIterator begin, TIterator end);
Abandon the idea of
std::vector< std::shared_ptr<const T> >
- Store an object by value.
- Store many objects in a container by value.
- If nothing else works, use smart pointers.
请参见 Bjarne Stroustrup - C ++的本质:使用C ++中的示例84,C ++ 98,C ++ 11和C ++ 14 在0:37:40。
For a long time I was using std::vector
and std::shared_ptr
hand in hand. Recently I started using std::shared_ptr<const T>
whenever a pointer to a const object was needed. This is all OK, since std::shared_ptr<T>
can be cast to std::shared_ptr<const T>
and then they share the same reference counter and everything feels natural.
But when I try to use constructs such as std::vector< std::shared_ptr<const T> >
I run into troubles. To simplify I will denote the two structures:
template <class T>
using SharedPtrVector = std::vector< std::shared_ptr<T> >;
template <class T>
using SharedConstPtrVector = std::vector< std::shared_ptr<const T> >;
The problem is that although SharedPtrVector
and SharedConstPtrVector
are very similar, SharedConstPtrVector
cannot be cast to SharedPtrVector
.
So each time I want to be a const correct and write a function such as:
void f(const SharedConstPtrVector<T>& vec);
there is no way I can pass const SharedPtrVector<T>
to f
.
I was thinking about this a lot and considered several alternatives:
The problem with 1. is the computational overhead and increased uglyness of code, while 2. gives the code an "everything is a template" flavor.
I am an inexperienced programmer and I don't want to set out in the wrong direction. I would like to hear advice from someone who has experience with this problem.
I would suggest reviewing your design with a view to establish a clear owner of those object. This is the absence of clear ownership that lead people to use shared smart pointers.
Bjarne Stroustrup recommends using smart pointers only as a last resort. His recommendations (best to worst) are:
See Bjarne Stroustrup - The Essence of C++: With Examples in C++84, C++98, C++11, and C++14 at 0:37:40.
这篇关于使用std :: vector< std :: shared_ptr< const T> >反模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!