相关疑难解决方法(0)

353
推荐指数
18
解决办法
30万
查看次数

根据类模板的typename生成一个字符串?

我希望能做什么......

我有一个模板化的类,它根据作为类型参数传递的对象类型设置(命名)共享内存池.我想知道是否可能通过预处理器运算符或其他方式,有一种方法来"字符串化"类型名称,并附加一个标识符?

语法的一些东西:<classtype> _identifier
在哪里MyClass<int>生成int_identifier...
例如:

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 this is the equivalent of typing:
        m_typeName = "T_ObjectName";
        // ...When what we really want is something like:
        m_typeName = "int_ObjectName";
    }
    ~MyClass();        
};
Run Code Online (Sandbox Code Playgroud)


实现此功能对于完全根据作为类型参数传递的对象类型来命名,创建和管理伪独特内存池非常有用.

这样的事情可能吗?

此外,是否有可能解决这一类型名,它前面加上一个标识符WITHOUT被"stringized"(即创建一个typedef 命名的 intObjectName)?

c++ macros templates class c-preprocessor

2
推荐指数
1
解决办法
2619
查看次数

标签 统计

c++ ×2

c++11 ×1

c-preprocessor ×1

class ×1

macros ×1

templates ×1

typeof ×1

variables ×1