Specialization of a function template in another class / namespace?

NOTE. This question is only related to tinyxml, however, such details may help to better illustrate the concept.

I wrote a function template that will iterate over child nodes of XML child nodes, extract the value of the child element, and then transfer that child value of the element to the vector.

The "get value" part is also written as a function template:

i.e.

template <typename Type>
Type getXmlCollectionItem(
    const char* elementName, TiXmlNode* child, TiXmlNode* parent);

There are specialized functions for the search part, for returning the values ​​of various types of child elements, for example. std :: string and other user objects.

i.e.

template <>
std::string getXmlCollectionItem<std::string>(
    const char* elementName, TiXmlNode* child, TiXmlNode* parent);

template <>
MyObject getXmlCollectionItem<MyObject>(
    const char* elementName, TiXmlNode* child, TiXmlNode* parent);

All this works fine, but it seemed to me that it would be very useful to have functions in the shared library when working with tinyxml files.

: , . namespace UtilityFunctions, - , 'MyObject', , , 'MyObject'?

, , , , ...

, - . ( ), .

+5
2

, ( , , ).

, , .

, :

namespace A { namespace B {
  template <typename T> int foo(T) {throw 1;}
}}

template <> int A::B::foo(int) {throw 0;}

: http://www.comeaucomputing.com/tryitout/

"ComeauTest.c", line 5: error: the initial explicit specialization of function
          "A::B::foo(T) [with T=int]" must be declared in the namespace
          containing the template
  template <> int A::B::foo(int) {throw 0;} 
                        ^

:

namespace A { namespace B {
  template <typename T> int foo(T) {throw 1;}
}}

namespace A { namespace B {
  template <> int foo(int) {throw 0;}
}}

, ?

, , , ( ), , ADL . , , .

:

namespace A { namespace B {
  template <typename T> int bar(T t) {return 0;}
  template <typename T> int foo(T t) {return bar(t);}
}}

namespace C {
  struct Bah {};
  int bar(Bah&) {return 1;}
}


int main(int argc,char** argv) 
{
  C::Bah bah;

  std::cout << A::B::foo(0) << std::endl;
  std::cout << A::B::foo(bah) << std::endl;
}

,

+4

: " , "

/ , 12- FAQ

+1

All Articles