相关疑难解决方法(0)

C++模板专门化,可以明确地调用可以作为指针或引用的类型的方法

摘要

有没有办法在模板化类型上调用类方法,该模板可能是指针或引用而不知道哪个而不是获取编译器/链接器错误?


细节

我有一个模板化的QuadTree实现,可以采用以下任何非平凡的用户定义类型:

//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
Run Code Online (Sandbox Code Playgroud)

但它们可能是指针或引用,因为它们都是从a2de :: Shape派生的.因此专业化被声明为:

template class QuadTree<a2de::Shape&>;
//...similar for all derived types as references.

template class QuadTree<a2de::Shape*>;
//...similar for all derived types as pointers
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是当间接(或缺少)未知时调用类方法的能力,并且由于模板,生成了两组代码:

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" …
Run Code Online (Sandbox Code Playgroud)

c++ templates pointers reference template-specialization

30
推荐指数
1
解决办法
1万
查看次数

模板化类函数T:如何判断T是否为指针?

作为这个问题的后续:我需要在这样的类函数中做出决定:

template< typename T > bool Class::Fun <T*> ( T& variable ) {...}
Run Code Online (Sandbox Code Playgroud)

T是否是指针.

在上面引用的问题中,答案是使用部分模板专业化.据我所知,这对于类功能是不可能的.这是真的?如果是这样,是否有另一种方法可以找出T是否为指针?

c++ templates type-traits

6
推荐指数
2
解决办法
1384
查看次数