Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   How to avoid splitting namespace scope? (http://www.velocityreviews.com/forums/t806068-how-to-avoid-splitting-namespace-scope.html)

Jayden Shui 11-19-2011 02:58 AM

How to avoid splitting namespace scope?
 
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


laird@lbreyer.com 11-19-2011 04:55 AM

Re: How to avoid splitting namespace scope?
 
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.

Jayden Shui 11-19-2011 05:27 AM

Re: How to avoid splitting namespace scope?
 
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

laird@lbreyer.com 11-19-2011 06:19 AM

Re: How to avoid splitting namespace scope?
 
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.

Bo Persson 11-19-2011 09:55 AM

Re: How to avoid splitting namespace scope?
 
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




All times are GMT. The time now is 04:55 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.