C ++模板专业化,明确地调用可能是指针或引用的类型的方法 [英] C++ template specialization, calling methods on types that could be pointers or references unambiguously

查看:83
本文介绍了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> must have class/struct/union.

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类的声明和用法中),我将再次得到上述的reference to pointer is illegal.

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

推荐答案

小的重载函数可用于将 reference 转换为 pointer :

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)始终是 pointer ,可用于调用成员函数,如上所示.

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)是指针,这意味着检查NULL是个好主意:

Since ptr(elem) is pointer, which means checking it for NULL be good idea:

 if( ptr(elem) && (ptr(elem)->Intersects(_bounds) == false)) return false;

希望有帮助.

这篇关于C ++模板专业化,明确地调用可能是指针或引用的类型的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆