How to avoid splitting namespace scope?

Discussion in 'C++' started by Jayden Shui, Nov 19, 2011.

  1. Jayden Shui

    Jayden Shui Guest

    Hello All,

    I give every class a tag by specialize the following template
    function:

    template<class T>
    std::string Tag();

    However, when working on a class in a namespace such as

    namespace S
    {
    class A { /*codes for A*/ };

    template<>
    std::string Tag<A>()
    {
    return "A";
    }

    // other codes
    }

    The compiler give an error. I have to specialize it in global scope
    like

    namespace S
    {
    class A { /*codes for A*/ };
    }

    template<>
    std::string Tag<S::A>()
    {
    return "A";
    }

    namespace S
    {
    // other codes
    }

    I don't want split the namespace scope by the tag specialization.
    Anyway I can make it work without the splitting?

    Thank you very much for your kind help!

    Best regards,

    Jayden
    Jayden Shui, Nov 19, 2011
    #1
    1. Advertising

  2. Jayden Shui

    Guest

    If all you want is a string describing the class, have you considered using the typeid operator?

    S::A a;
    cout << typeid(a).name() << endl;

    or create a single global Tag function template:

    template<class T> string Tag(T& t) { return typeid(a).name(); }

    Laird Breyer.
    , Nov 19, 2011
    #2
    1. Advertising

  3. Jayden Shui

    Jayden Shui Guest

    On Nov 18, 11:55 pm, wrote:
    > If all you want is a string describing the class, have you considered using the typeid operator?
    >
    > S::A a;
    > cout << typeid(a).name() << endl;
    >
    > or create a single global Tag function template:
    >
    > template<class T> string Tag(T& t) { return typeid(a).name(); }
    >
    > Laird Breyer.


    Thanks. But I'd like the tag more readable. Any other way?

    Best regards,

    Jayden
    Jayden Shui, Nov 19, 2011
    #3
  4. Jayden Shui

    Guest

    The name() string's format depends on your compiler.

    With Visual C++ the string should be something like "class S::A".

    With GCC, the name is mangled like "N1S1AE" and the string can be demangled using c++filt

    $ c++filt -t N1S1AE
    S::A

    See also
    http://gcc.gnu.org/onlinedocs/libstdc /manual/bk01pt12ch39.html

    Laird.
    , Nov 19, 2011
    #4
  5. Jayden Shui

    Bo Persson Guest

    Jayden Shui wrote:
    > Hello All,
    >
    > I give every class a tag by specialize the following template
    > function:
    >
    > template<class T>
    > std::string Tag();
    >
    > However, when working on a class in a namespace such as
    >
    > namespace S
    > {
    > class A { /*codes for A*/ };
    >
    > template<>
    > std::string Tag<A>()
    > {
    > return "A";
    > }
    >
    > // other codes
    > }
    >
    > The compiler give an error. I have to specialize it in global scope
    > like
    >
    > namespace S
    > {
    > class A { /*codes for A*/ };
    > }
    >
    > template<>
    > std::string Tag<S::A>()
    > {
    > return "A";
    > }
    >
    > namespace S
    > {
    > // other codes
    > }
    >
    > I don't want split the namespace scope by the tag specialization.
    > Anyway I can make it work without the splitting?
    >


    No really. You cannot specialize a template from one namespace inside
    another namespace.


    Bo Persson
    Bo Persson, Nov 19, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Ericson
    Replies:
    0
    Views:
    424
    John Ericson
    Jul 19, 2003
  2. Mark
    Replies:
    0
    Views:
    440
  3. John Dibling
    Replies:
    0
    Views:
    412
    John Dibling
    Jul 19, 2003
  4. Anonymous
    Replies:
    3
    Views:
    531
    Ron Natalie
    Aug 18, 2003
  5. Steven T. Hatton
    Replies:
    9
    Views:
    477
Loading...

Share This Page