3

给定一般形式的 POD 结构

struct case_0   { const char *foo;                       };
struct case_1i  { const char *foo; int v0;               };
struct case_1d  { const char *foo; double v0;            };
struct case_2ii { const char *foo; int v0;    int v1;    };
struct case_2id { const char *foo; int v0;    double v1; };
// etc

是否可以根据 , 等数据成员的存在与否来调度函数重载集的(模板)成员——理想情况下,不依赖这些成员的特定类型——如果可以,如何?具体来说,给定v0v1

void
process(const case_0& c)
{
   do_stuff_with(c.foo);
}

template <typename case_1> void   
process(const case_1& c)
{
   do_stuff_with(c.foo, c.v0);
}

template <typename case_2> void
process(const case_2& c)
{
   do_stuff_with(c.foo, c.v0, c.v1);
}

我希望为所有在其主体中使用case_*的所有成员的结构选择每个重载v-,并且 - 同样重要的是 -没有任何v-未在其主体中使用的成员。

这个程序必须是 100% 独立的,所以请不要 Boost。C++11 功能还可以。

4

2 回答 2

3

You need to write a set of traits such as has_v0 and has_v1 (which I'm sure has been demonstrated many times on SO) then constrain your overloads using them:

template <typename case_0,
  typename = typename std::enable_if<!has_v0<case_0>::value>::type,
  typename = typename std::enable_if<!has_v1<case_0>::value>::type
>
void
process(const case_0& c)
{
   do_stuff_with(c.foo);
}

template <typename case_1,
  typename = typename std::enable_if<has_v0<case_1>::value>::type,
  typename = typename std::enable_if<!has_v1<case_1>::value>::type
>
void   
process(const case_1& c)
{
   do_stuff_with(c.foo, c.v0);
}

template <typename case_2,
  typename = typename std::enable_if<has_v0<case_2>::value>::type,
  typename = typename std::enable_if<has_v1<case_2>::value>::type
>
void
process(const case_2& c)
{
   do_stuff_with(c.foo, c.v0, c.v1);
}

You can simplify the constraints with something like

template<typename Cond>
  using Require = typename std::enable_if<Cond::value>::type;

e.g.

template <typename case_2,
  typename = Require<has_v0<case_2>>,
  typename = Require<has_v1<case_2>>
>
void
process(const case_2& c)
{
   do_stuff_with(c.foo, c.v0, c.v1);
}
于 2013-01-08T19:07:50.780 回答
2

One solution is provided by @Jonathan Wakely which employs the use of has_XXX meta-functions.

Here is another solution, but it requires you to change your full-fledge structs definitions to mere typedefs of std::tuple<>.

full-fledge structs:

struct case_0   { const char *foo;                       };
struct case_1i  { const char *foo; int v0;               };
struct case_1d  { const char *foo; double v0;            };
struct case_2ii { const char *foo; int v0;    int v1;    };
struct case_2id { const char *foo; int v0;    double v1; };

are replaced with typedefs structs as follows:

typedef std::tuple<const char*>            case_0;
typedef std::tuple<const char*,int>        case_1i;
typedef std::tuple<const char*,double>     case_1d;
typedef std::tuple<const char*,int,int>    case_2ii;
typedef std::tuple<const char*,int,double> case_2id;

template<typename...Args>
auto foo(std::tuple<Args...> & tpl) -> decltype(std::get<0>(tpl))&
{
     return std::get<0>(tpl);
}

template<typename...Args>
auto v0(std::tuple<Args...> & tpl) -> decltype(std::get<1>(tpl))&
{
     return std::get<1>(tpl);
}

template<typename...Args>
auto v1(std::tuple<Args...> & tpl) -> decltype(std::get<2>(tpl))&
{
     return std::get<2>(tpl);
}

and the usage

case_1i obj; //full-fledge struct
obj.foo = "hello";
obj.v0 = 100;

is replaced with

case_1i obj; //typedef struct
foo(obj) = "hello";
v0(obj) = 100;

Once you accept this design change, the solution to your original problem becomes pretty much straight-forward as follows:

template<size_t...>
struct seq{};

template<size_t M, size_t ...N>
struct genseq  : genseq<M-1,M-1, N...> {};

template<size_t ...N>
struct genseq<0,N...>
{
    typedef seq<N...> type;
};

template <typename ...Args, size_t ...N> 
void call_do_stuff_with(std::tuple<Args...> & tpl, seq<N...>)
{
    do_stuff_with(std::get<N>(tpl)...);
}

template <typename ...Args> 
void process(std::tuple<Args...> & tpl)
{
   const size_t N = sizeof ...(Args);
   call_do_stuff_with(tpl, typename genseq<N>::type());
}

Do let me know if that is acceptable. If that is not acceptable, I will delete my answer (if you feel so).

Live demo!

于 2013-01-08T20:07:10.537 回答