Home  |  About  | Last |  Submit  |  Contact
AllQuests.com

Previous Question:  Memory losse and thread advice  Visual C++ ProgrammingNext Question:  question if u wouldnt mind  Just Starting Your Design
Question Can pass string literal for template parameter ( CodeGuru Forums C++ (Non Visual C++ Issues) )
Updated: 2008-08-12 07:15:11 (3)
Can pass string literal for template parameter

Hi

I'm gonna implement a dynamic type-cast mechanism. I need to give unique ID to all classes, and I think hash value calculated from the name of class would be appropriate.

What I'd like to do is something like this:
Code:
template <char const* const psz> struct _hash_ { enum { value = *psz ? (_hash_<psz+1>::value << 2) | *psz : 0 }; }; ......... enum { _classid = _hash_<"cMyClass">::value };


It produces compile errors. I can imagine the reason, (I'm afraid this is fairly stupid idea :S) but I'd like to know if there's any way around.

Thanks in advance

Answers: Can pass string literal for template parameter ( CodeGuru Forums C++ (Non Visual C++ Issues) )
Can pass string literal for template parameter

Quote:
Originally Posted by muse1987
Hi

I'm gonna implement a dynamic type-cast mechanism. I need to give unique ID to all classes, and I think hash value calculated from the name of class would be appropriate.
You have typeid() to get you a compiler-defined name for your classes.

Regards,

Paul McKenzie

Paul McKenzie

Can pass string literal for template parameter

Code:
template <char const* const psz> struct _hash_ { enum { value = *psz ? (_hash_<psz+1>::value << 2) | *psz : 0 }; };



Should be :

Code:
template <const char const* psz>



I hope this help.

Peter_APIIT

Can pass string literal for template parameter

Why don't you make the struct _hash_ as a function that takes a const char*? And no, you cannot templatize on string literals.



Previous Question:  Memory losse and thread advice  CodeGuru Forums  Visual C++ ProgrammingNext Question:  question if u wouldnt mind  SitePoint Forums  Just Starting Your Design

- Source: Can pass string literal for template parameter CodeGuru Forums C++ (Non Visual C++ Issues)
- Previous Question: Memory losse and thread advice CodeGuru Forums Visual C++ Programming
- Next Question: question if u wouldnt mind SitePoint Forums Just Starting Your Design





AllQuests.com