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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With