c++ - Generate a string based upon a class template's typename? -


what i'd able do...

i have templated class sets (named) shared memory pool based upon type of object passed type parameter. wondering if, possibly through aid of preprocessor operators or otherwise, there way "stringize" typename, , append identifier?

something along lines of syntax: <classtype>_identifier
myclass<int> generate int_identifier...
example:

template<typename t> class myclass { private:     #define typename_string(s) t_#s     std::string m_typename;  public:     myclass(std::string objname = "objectname")     {         // this:         m_typename = typename_string(objname.c_str());         // ...obviously doesn't work, since equivalent of typing:         m_typename = "t_objectname";         // ...when want like:         m_typename = "int_objectname";     }     ~myclass();         }; 


getting work useful name, create , manage psuedo-unique memory pools based entirely upon type of object being passed type parameter.

is possible?

also, possible resolve typename , prepend identifier without being "stringized" (i.e., create typedef named intobjectname)?

no, not possible, @ least not @ level. macros evaluated before compilation, is, before template type parameters t in example evaluated. this:

#include <string> #include <memory> template <class t> struct tinfo; template <class t> class myclass;  #define tinfo(type)                                \ template <> struct tinfo<type> {                   \   static char const* getname() {                   \     return #type;                                  \   }                                                \ };                                                 \ typedef myclass<type> type##_class;                \ typedef std::unique_ptr<myclass<type>> type##_uptr;    template <class t> class myclass { private:   std::string m_typename;  public:   myclass(std::string objname = "objectname")      : m_typename(std::string(tinfo<t>::getname()) + "_" + objname)   {}    std::string const& getname() {     return m_typename;   } };   //usage: #include <iostream>  tinfo(int); int main() {   int_uptr pi(new int_class());   std::cout << pi->getname() << '\n'; } 

see http://ideone.com/3ivyqv


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -