本文介绍了tuple_size和来自tuple的inhereted类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
class TR_AgentInfo : public tuple<
long long, //AgentId
string, //AgentIp
>
{
public:
TR_AgentInfo() {}
TR_AgentInfo(
const long long& AgentId,
const string& AgentIp,
)
{
get<0>(*this) = AgentId;
get<1>(*this) = AgentIp;
}
long long getAgentId() const { return get<0>(*this); }
void setAgentId(const long long& AgentId) { get<0>(*this) = AgentId; }
string getAgentIp() const { return get<1>(*this); }
void setAgentIp(const string& AgentIp) { get<1>(*this) = AgentIp; }
};
现在我想使用这个代码:
Now I want to use this code:
int count = tuple_size<TR_AgentInfo>::value;
,但gcc会出现此错误:
but gcc give this error:
error: incomplete type std::tuple_size<TR_AgentInfo> used in nested name specifier
现在我该怎么办?
推荐答案
namespace std
{
template<> struct tuple_size<TR_AgentInfo>
{
static const size_t value = 2;
// alternatively, `tuple_size<tuple<long long, string>>::value`
// or even better, `tuple_size<TR_AgentInfo::tuple_type>::value`, #1
};
}
您明确允许向命名空间添加专门化
You are expressly allowed to add specializations to the namespace
std
, precisely for situations like yours.
如果你的实际类本身是模板,你可以简单地替换
2
。例如,对于建议#1,您可以为您的类添加 tuple_type
的typedef。有很多方法来皮肤这只猫。
If your actual class is itself templated, you can simply replace
2
by an appropriate construction. For example, for suggestion #1 you could add a typedef for tuple_type
to your class. There are many ways to skin this cat.
这篇关于tuple_size和来自tuple的inhereted类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!