tuple_size and an inhereted class from tuple?

自古美人都是妖i 提交于 2019-12-06 11:45:29

If you just want your one class to work with std::tuple_size, you can simply provide a specialization:

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.

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.

If you have an instance of TR_AgentInfo kicking around, you can use function template type deduction to make it friendlier, like so:

template <typename... T>
std::size_t get_tuple_size(std::tuple<T...> const &)
{
  return sizeof...(T);
}

and call

TR_AgentInfo info;
int count = get_tuple_size(info);

otherwise you need to use proper metaprogramming to get at the base class, but facilities are available.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!