Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to avoid splitting namespace scope?

Reply
Thread Tools

How to avoid splitting namespace scope?

 
 
Jayden Shui
Guest
Posts: n/a
 
      11-19-2011
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

 
Reply With Quote
 
 
 
 
laird@lbreyer.com
Guest
Posts: n/a
 
      11-19-2011
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.
 
Reply With Quote
 
 
 
 
Jayden Shui
Guest
Posts: n/a
 
      11-19-2011
On Nov 18, 11:55*pm, (E-Mail Removed) 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
 
Reply With Quote
 
laird@lbreyer.com
Guest
Posts: n/a
 
      11-19-2011
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/libstd...1pt12ch39.html

Laird.
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      11-19-2011
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Avoid wasting time or how to avoid initialization Alexander Malkis C++ 8 04-13-2004 11:23 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments