开发者

Deduce parameter pack w/out passing it explicitly as a template parameter?

Forgive any ignorance of C++ type deduction in this, but I'd like to be able to carry around the parameter pack's definition, so that later I could test for an inner type. Is this possible? Something like:

template <typename... Args> struct Entity {

    struct Inner {
        typedef Args... entity_args_t;
    };

    struct SomeOtherInner {
        typedef Args... entity_args_t;
    };
};

struct ThingA : Entity<int, string> {
};

struct ThingB : Entity<开发者_开发知识库string, string> {
};

//Want to accept variations of Entity<...>::Inner,
//not Entity<...>::SomeOtherInner
template<typename I>
struct is_Entity_Inner {
    static const bool value
        = is_same<
            typename Entity<typename I::entity_args_t...>::Inner
            , I
        >::value
    ;
};

Oui? Non?


Define:

template<typename ...> struct types;

Then:

template <typename... Args> struct Entity {

    struct Inner {
        typedef types<Args...> entity_args_t;
    };

    struct SomeOtherInner {
        typedef types<Args...> entity_args_t;
    };
};

Then you can pass entity_args_t to a template that has a partial specialization for types<T...>. If you typedef the Entity, you can instead write a partial specialization for Entity<T...>, which may make more sense for your case

template <typename... Args> struct Entity {

    struct Inner {
        // Equivalent: typedef Entity entity_args_t;
        typedef Entity<Args...> entity_args_t;
    };

    struct SomeOtherInner {
        typedef Entity<Args...> entity_args_t;
    };
};

So having a typedef for entity_args_t equal Entity<Args...>, you could write this as follows (untested, but should work):

template<typename ProbablyInner, typename ProbablyEntity>
struct is_inner_impl : std::false_type 
{ };

template<typename ProbablyInner, typename ...Args>
struct is_inner_impl<ProbablyInner, Entity<Args...>> 
  : std::is_same<
      typename Entity<Args...>::Inner
      ProbablyInner>
{ };

template<typename ProbablyInner, typename = std::true_type>
struct is_inner : std::false_type 
{ };

template<typename ProbablyInner>
struct is_inner<ProbablyInner, 
  std::integral_constant<bool, is_inner_impl<
    ProbablyInner, 
    typename ProbablyInner::entity_args_t>::value>>
  : std::true_type 
{ };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜