• <tfoot id='9snJT'></tfoot>

  • <small id='9snJT'></small><noframes id='9snJT'>

    1. <i id='9snJT'><tr id='9snJT'><dt id='9snJT'><q id='9snJT'><span id='9snJT'><b id='9snJT'><form id='9snJT'><ins id='9snJT'></ins><ul id='9snJT'></ul><sub id='9snJT'></sub></form><legend id='9snJT'></legend><bdo id='9snJT'><pre id='9snJT'><center id='9snJT'></center></pre></bdo></b><th id='9snJT'></th></span></q></dt></tr></i><div id='9snJT'><tfoot id='9snJT'></tfoot><dl id='9snJT'><fieldset id='9snJT'></fieldset></dl></div>
    2. <legend id='9snJT'><style id='9snJT'><dir id='9snJT'><q id='9snJT'></q></dir></style></legend>
        <bdo id='9snJT'></bdo><ul id='9snJT'></ul>

        C++ 模板特化,明确调用可以是指针或引用的类型的方法

        C++ template specialization, calling methods on types that could be pointers or references unambiguously(C++ 模板特化,明确调用可以是指针或引用的类型的方法)

            <tbody id='Y8F1G'></tbody>
        1. <small id='Y8F1G'></small><noframes id='Y8F1G'>

                <bdo id='Y8F1G'></bdo><ul id='Y8F1G'></ul>
              • <i id='Y8F1G'><tr id='Y8F1G'><dt id='Y8F1G'><q id='Y8F1G'><span id='Y8F1G'><b id='Y8F1G'><form id='Y8F1G'><ins id='Y8F1G'></ins><ul id='Y8F1G'></ul><sub id='Y8F1G'></sub></form><legend id='Y8F1G'></legend><bdo id='Y8F1G'><pre id='Y8F1G'><center id='Y8F1G'></center></pre></bdo></b><th id='Y8F1G'></th></span></q></dt></tr></i><div id='Y8F1G'><tfoot id='Y8F1G'></tfoot><dl id='Y8F1G'><fieldset id='Y8F1G'></fieldset></dl></div>
                • <tfoot id='Y8F1G'></tfoot>

                  <legend id='Y8F1G'><style id='Y8F1G'><dir id='Y8F1G'><q id='Y8F1G'></q></dir></style></legend>
                  本文介绍了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
                  //Derived Classes

                  但它们可以是指针或引用,因为它们都是从 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.


                  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;


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



                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                • <tfoot id='xDJJ3'></tfoot>
                    <tbody id='xDJJ3'></tbody>

                    <legend id='xDJJ3'><style id='xDJJ3'><dir id='xDJJ3'><q id='xDJJ3'></q></dir></style></legend>

                    <small id='xDJJ3'></small><noframes id='xDJJ3'>

                    • <bdo id='xDJJ3'></bdo><ul id='xDJJ3'></ul>

                            <i id='xDJJ3'><tr id='xDJJ3'><dt id='xDJJ3'><q id='xDJJ3'><span id='xDJJ3'><b id='xDJJ3'><form id='xDJJ3'><ins id='xDJJ3'></ins><ul id='xDJJ3'></ul><sub id='xDJJ3'></sub></form><legend id='xDJJ3'></legend><bdo id='xDJJ3'><pre id='xDJJ3'><center id='xDJJ3'></center></pre></bdo></b><th id='xDJJ3'></th></span></q></dt></tr></i><div id='xDJJ3'><tfoot id='xDJJ3'></tfoot><dl id='xDJJ3'><fieldset id='xDJJ3'></fieldset></dl></div>