gpt4 book ai didi

c++ - 具有嵌套类的可变长度参数模板类

转载 作者:搜寻专家 更新时间:2023-10-31 01:37:32 26 4
gpt4 key购买 nike

我正在尝试编译它,但在嵌套类方面遇到了问题。

struct TKey {
char a[2];
};

template<class TKEY, class TOBJ>
struct CHash {
struct TNode {
TKEY Key;
TOBJ Obj;
};
int stuff;
};

template <class TKEY, class... ARGS>
class CNested : public CHash<TKEY, int> {
public:
typedef CNested<ARGS...> TNested;

template<class TKEY1, class... ARGS1>
struct TNodeType {
typedef typename TNested::TNodeType<ARGS1...>::Type Type;
};

template<class TKEY>
struct TNodeType<TKEY> {
typedef TNode Type;
};

CNested() { }

};

// recursive template, tail
template <class TKEY, class TOBJ>
class CNested<TKEY, TOBJ> : public CHash<TKEY, TOBJ> {
public:
CNested() { }
};


int main(int argc, char* argv[])
{
// p should have type of CNested<TKey, TKey, int>::TNode*
CNested<TKey, TKey, TKey, int>::TNodeType<TKey>* p;
return 0;
}

最佳答案

TNodeType 是依赖模板名称,因此您需要:

typedef typename TNested::template TNodeType<ARGS1...>::Type Type;
^^^^^^^^

同样在嵌套结构 TNodeType 参数 TKEY 中隐藏了外部 class CNested 的参数 TKEY,所以你需要更改为:

template<class TKEY1>
struct TNodeType<TKEY1> {
typedef TNode Type;
};

关于c++ - 具有嵌套类的可变长度参数模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34007013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com