C++ class 模板专门化变量
C++ class template specialize variable
我有 Pool
class 模板,可以这样说:
template <class T>
class Pool {
public:
static int iPoolUpperBound;
static int iPoolSize;
static T **pItem;
T();
~T();
}
当我需要某个 class 的对象池时,我只使用该模板。我也有 Item
class,我需要将矢量添加到当前 Pool
模板,但是该向量应该只存在于 im referring/using Item
class.
简单的 if (T == Item)
显然行不通,我已经尽力有条件地将向量添加到 Pool
模板
如果我可以有条件地在 Pool
class 模板成员函数中添加一两行而不重载它,那也会很有帮助。
It would be also helpful if i could conditionally add line or two in Pool class template member function without overloading it.
例如,您只能重载 pItem
。
struct Item
{ };
template <typename>
struct proItem
{ };
template <>
struct proItem<Item>
{ static Item ** pItem; };
Item ** proItem<Item>::pItem;
template <typename T>
struct Pool : public proItem<T>
{
static int iPoolUpperBound;
static int iPoolSize;
};
int main()
{
Pool<Item>::pItem = nullptr; // compile
//Pool<int>::pItem = nullptr; // compilation error
}
我有 Pool
class 模板,可以这样说:
template <class T>
class Pool {
public:
static int iPoolUpperBound;
static int iPoolSize;
static T **pItem;
T();
~T();
}
当我需要某个 class 的对象池时,我只使用该模板。我也有 Item
class,我需要将矢量添加到当前 Pool
模板,但是该向量应该只存在于 im referring/using Item
class.
简单的 if (T == Item)
显然行不通,我已经尽力有条件地将向量添加到 Pool
模板
如果我可以有条件地在 Pool
class 模板成员函数中添加一两行而不重载它,那也会很有帮助。
It would be also helpful if i could conditionally add line or two in Pool class template member function without overloading it.
例如,您只能重载 pItem
。
struct Item
{ };
template <typename>
struct proItem
{ };
template <>
struct proItem<Item>
{ static Item ** pItem; };
Item ** proItem<Item>::pItem;
template <typename T>
struct Pool : public proItem<T>
{
static int iPoolUpperBound;
static int iPoolSize;
};
int main()
{
Pool<Item>::pItem = nullptr; // compile
//Pool<int>::pItem = nullptr; // compilation error
}