C++ 模板特化,明确调用可以是指针或引用的类型的方法
总结
有没有一种方法可以在不知道是指针或引用的模板化类型上调用类方法,并且不会出现编译器/链接器错误?
Is there a way to call a class method on a templated type that could be a pointer or a reference without knowing which and not get compiler/linker errors?
详情
我有一个模板化的 QuadTree 实现,它可以采用以下任何非平凡的用户定义类型:
I have a templated QuadTree implementation that can take any of the following non-trivial user-defined types:
//Abstract Base Class
a2de::Shape
//Derived Classes
a2de::Point
a2de::Line
a2de::Rectangle
a2de::Circle
a2de::Ellipse
a2de::Triangle
a2de::Arc
a2de::Spline
a2de::Sector
a2de::Polygon
但它们可以是指针或引用,因为它们都是从 a2de::Shape 派生的.所以特化声明为:
But they could be a pointer OR a reference as they are all derived from a2de::Shape. So the specializations are declared as:
template class QuadTree<a2de::Shape&>;
//...similar for all derived types as references.
template class QuadTree<a2de::Shape*>;
//...similar for all derived types as pointers
我遇到的问题是当间接(或缺少间接)未知并且由于模板而生成两组代码时,能够调用类方法:
The problem I am having is the ability to call a class method when the indirection (or lack thereof) is unknown and due to the templates, both sets of code are generated:
template<typename T>
bool QuadTree<T>::Add(T& elem) {
//When elem of type T is expecting a pointer here
//-> notation fails to compile where T is a reference i.e.:
//template class QuadTree<a2de::Shape&>
//with "pointer to reference is illegal"
if(elem->Intersects(_bounds) == false) return false;
//...
}
如果我更改上面的行以使用 .(点)符号:
If I change the above line to use the . (dot) notation:
template<typename T>
bool QuadTree<T>::Add(T& elem) {
//When elem of type T is expecting a reference here
//. (dot) notation fails to compile where T is a pointer i.e.:
//template class QuadTree<a2de::Shape*>
//with "pointer to reference is illegal"
if(elem.Intersects(_bounds) == false) return false;
//...
}
如果我删除基于引用的类型以支持基于指针的类型(包括在 Quadtree 类的声明和使用中),我会收到错误 left of .<function-name>必须有类/结构/联合
.
If I remove the reference-based types in favor of the pointer-based types (including in the declaration and usage of the Quadtree class) I get the error left of .<function-name> must have class/struct/union
.
如果我删除基于指针的类型以支持基于引用的类型(包括在 Quadtree 类的声明和使用中),我将再次收到上述 对指针的引用是非法的
.
If I remove the pointer-based type in favor of the reference-based types (including in the declaration and usage of the Quadtree class) I get the aforementioned reference to pointer is illegal
again.
编译器:VS2010-SP1
compiler: VS2010-SP1
推荐答案
可以使用小重载函数将引用变成指针:
Small overloaded functions can be used to turn reference into pointer:
template<typename T>
T * ptr(T & obj) { return &obj; } //turn reference into pointer!
template<typename T>
T * ptr(T * obj) { return obj; } //obj is already pointer, return it!
现在不要这样做:
if(elem->Intersects(_bounds) == false) return false;
if(elem.Intersects(_bounds) == false) return false;
这样做:
if( ptr(elem)->Intersects(_bounds) == false) return false;
如果 elem
是一个引用,第一个重载 ptr
将被选中,否则第二个将被选中.两者都返回 pointer,这意味着无论代码中的 elem
是什么,表达式 ptr(elem)
将始终是一个 指针 可以用来调用成员函数,如上所示.
If elem
is a reference, the first overload ptr
will be selected, else the second will be selected. Both returns pointer, which means irrespective of what elem
is in your code, the expression ptr(elem)
will always be a pointer which you can use to invoke the member functions, as shown above.
因为 ptr(elem)
是指针,这意味着检查 nullptr
是个好主意:
Since ptr(elem)
is pointer, which means checking it for nullptr
be good idea:
if( ptr(elem) && (ptr(elem)->Intersects(_bounds) == false)) return false;
希望有所帮助.
相关文章