[英]How should you implement STL features for custom classes?
I'm implementing a tuple-like class, call it MyTuple
, and I would like it to have some STL features, namely tuple_element
, tuple_size
, and get
. 我正在实现一个类似于元组的类,称为
MyTuple
,并且我希望它具有一些STL功能,即tuple_element
, tuple_size
和get
。
tuple_element
and tuple_size
specializations in namespace std
? namespace std
实现tuple_element
和tuple_size
专业化吗? If not then how should I implement them? get
function, does that mean I must provide a separate get
function in my own namespace, thereby forcing users to write using std::get;
get
函数,这是否意味着我必须在自己的名称空间中提供单独的get
函数,从而迫使用户using std::get;
进行编写using std::get;
? Recall that there ain't no such thing as a partial specialization of function templates. 回想一下,不存在功能模板的部分专业化。 So you can't partially specialize
std::tuple_element
et al for your class. 因此,您不能为您的班级专门研究
std::tuple_element
等。
Normally, you would define free-standing functions that are logically part of your class' interface in the same namespace as the class itself. 通常,您应该在与类本身相同的名称空间中定义逻辑上属于类接口的独立函数。 Then you can rely on argument-dependent lookup to find them.
然后,您可以依靠依赖于参数的查找来找到它们。 std::get would be found by that same mechanism - users of your library will not in fact be forced to write
using std::get
. std :: get可以通过相同的机制找到-实际上,不会强迫您的库用户
using std::get
进行写入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.