Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11611 > unrolled thread

How to avoid splitting namespace scope?

Started byJayden Shui <jayden.shui@gmail.com>
First post2011-11-18 18:58 -0800
Last post2011-11-19 10:55 +0100
Articles 5 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  How to avoid splitting namespace scope? Jayden Shui <jayden.shui@gmail.com> - 2011-11-18 18:58 -0800
    Re: How to avoid splitting namespace scope? laird@lbreyer.com - 2011-11-18 20:55 -0800
      Re: How to avoid splitting namespace scope? Jayden Shui <jayden.shui@gmail.com> - 2011-11-18 21:27 -0800
        Re: How to avoid splitting namespace scope? laird@lbreyer.com - 2011-11-18 22:19 -0800
    Re: How to avoid splitting namespace scope? "Bo Persson" <bop@gmb.dk> - 2011-11-19 10:55 +0100

#11611 — How to avoid splitting namespace scope?

FromJayden Shui <jayden.shui@gmail.com>
Date2011-11-18 18:58 -0800
SubjectHow to avoid splitting namespace scope?
Message-ID<1e180097-a703-4bc9-bf7d-b51afaf245ea@a5g2000vbb.googlegroups.com>
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

[toc] | [next] | [standalone]


#11612

Fromlaird@lbreyer.com
Date2011-11-18 20:55 -0800
Message-ID<15905453.1326.1321678541309.JavaMail.geo-discussion-forums@prmf13>
In reply to#11611
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.

[toc] | [prev] | [next] | [standalone]


#11613

FromJayden Shui <jayden.shui@gmail.com>
Date2011-11-18 21:27 -0800
Message-ID<0125ddd6-703f-4875-9424-cc0c083be824@m7g2000vbc.googlegroups.com>
In reply to#11612
On Nov 18, 11:55 pm, la...@lbreyer.com 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

[toc] | [prev] | [next] | [standalone]


#11614

Fromlaird@lbreyer.com
Date2011-11-18 22:19 -0800
Message-ID<28940880.0.1321683551375.JavaMail.geo-discussion-forums@prgt40>
In reply to#11613
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.

[toc] | [prev] | [next] | [standalone]


#11615

From"Bo Persson" <bop@gmb.dk>
Date2011-11-19 10:55 +0100
Message-ID<9ipcnjFg7oU1@mid.individual.net>
In reply to#11611
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web