How can I deduce the outer type of an inner type in C++?

陌路散爱 提交于 2019-12-18 08:48:28

问题


I have many classes exposing an inner type named Binding. For instance, one of them could be:

struct Message
{
    struct Binding
    {
    };
};

I invoke a function apply like this:

apply< Message >([](Message::Binding& x)
{
    // setup binding fields
});

for I wrote

template <class TMessage, class TBindingExpression>
void apply(const TBindingExpression& expr)
{
    typedef typename TMessage::Binding BindingType;

    BindingType binding;
    expr(binding);

    apply(MessageUtil::typeId< TMessage >(), binding);
}

Since Message is a bit redundant in the way I invoke apply, I would like to make the compiler deduce Message so I can write

apply([](Message::Binding x)
{
    //...
});

So far, I am stuck here:

template <class TBindingExpression>
void apply(const TBindingExpression& expr)
{
    // I get the type of the argument which is Message::Binding in this example
    typedef typename std::tuple_element
    <
        0,
        FunctionTraits< TBindingExpression >::ArgumentTypes
    >
    ::type BindingType;

    // so I can invoke my expression
    BindingType binding;
    expr(binding);

    // But now I need the type of the outer class, i.e. Message
    typedef typename MessageTypeFromBinding< BindingType >::Type MessageType;

    apply(MessageUtil::typeId< MessageType >(), binding);
}

Is there a way to write/achieve MessageTypeFromBinding?

Obviously, that's pure curiosity and cosmetic concerns.


回答1:


template<class T>struct inner_class_of{using outer_class=T;}; 

struct Message {
  struct Binding:inner_class_of<Message> {
  };
};

template<class T>
inner_class_of<T> get_outer_helper(inner_class_of<T>const&);

template<class T>
using outer_class_of_t = typename decltype(get_outer_helper(std::declval<T>()))::outer_class;

now outer_class_of_t<Message::Binding> is Message.

I made it a bit industrial strength, as it works even if Binding hides outer_class.

You can drop helper and rewrite outer_class_of_t=typename T::outer_class if you prefer.



来源:https://stackoverflow.com/questions/24206337/how-can-i-deduce-the-outer-type-of-an-inner-type-in-c

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