Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7081035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:52:08+00:00 2026-05-28T06:52:08+00:00

NOTE: This question is only loosely related to tinyxml, however including details like that

  • 0

NOTE: This question is only loosely related to tinyxml, however including details like that may help illustrate the concept better.

I have written a function template that will iterate through a parent XML nodes children, retrieve the value of the child element and then push that child element value to a vector.

The ‘retrieve the 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 specialisations for the retrieval part, for returning different types of child element value, e.g. std::string and other custom 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);

This all works perfectly well, however it struck me that this would be very useful to have in a shared function library when dealing with tinyxml files.

Question: Is it possible to declare a function template in one namespace e.g. namespace UtilityFunctions, which doesn’t have any knowledge of specific object types like 'MyObject', and then declare and define specialisations of that function template in other namespaces which do have the knowledge of specific object types like 'MyObject'?

My hunch is that it isn’t possible, but the concept of having a common function template seems to me to be useful enough for there to be an alternative way of getting close to the functionality I’m looking for…

Apologies if any of the terminology is incorrect or explanation is unclear. I’ve done a lot of research around this topic (to get to the point of working function template specialisation within the same namespace) but found no definitive answer as yet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:52:09+00:00Added an answer on May 28, 2026 at 6:52 am

    It is not possible to write in one namespace a specialization of a template defined in another namespace (since that would not be specialization of that template, being defined in another namespace it would be a different template).

    However it is perfectly OK to extend namespace where template has been originally defined, writing your specialization in totally separate source file.

    So here is what you cannot do:

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

    You can see nice error message for the above at 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;} 
                            ^
    

    Here is what you can do:

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

    Is there any reason why that would be a problem?

    Also, if you delegate the work to a function associated with the object you are reading (either member or free function), you can depend on that function being found via ADL and called. Meaning you should be able to minimize the amount of such specializations like the above.

    Here is example:

    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;
    }
    

    Edited to add an example

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please note that this question is from 2008 and now is of only historic
(note that this question is not about CAS, it's about the May fail spuriously
(Please note that the behavior described in this question only appeared because of something
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
This question may expose my ignorance as a web developer, but that wouldn't exactly
Note: This question is not about making a custom dropdown. It's only about possibilities
Note: This is a Java -only question (i.e. no Javascript, sed, Perl, etc.) I
I realize that this question spans many technologies, but I am only looking for
Please note that this question is about CGLayer (which you typically use to draw
(NOTE: This question is not about escaping queries, it's about escaping results) I'm using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.