4
我有很多類暴露內部類型名爲Binding
。例如,其中一個可能是:如何推斷C++中內部類型的外部類型?
struct Message
{
struct Binding
{
};
};
我調用一個函數apply
這樣的:
apply<Message>([](Message::Binding& x)
{
// setup binding fields
});
爲我寫
template <class TMessage, class TBindingExpression>
void apply(const TBindingExpression& expr)
{
typedef typename TMessage::Binding BindingType;
BindingType binding;
expr(binding);
apply(MessageUtil::typeId<TMessage>(), binding);
}
由於Message
是在路上有點多餘我調用apply
,我想讓編譯器演繹Message
,這樣我就可以編寫
apply([](Message::Binding x)
{
//...
});
到目前爲止,我被困在這裏:
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);
}
是否有寫/實現MessageTypeFromBinding
的方法嗎?
顯然,這純粹是好奇心和美容問題。
IIRC這是不可能的(類似於獲取聲明類型的名稱空間)。但是你可以在'Binding'中提供一個typedef。而且,函數對象通常按值傳遞;依賴函數特徵來獲得operator()或類似的第一個參數的* single *參數類型是危險的,考慮重載'operator()'甚至C++ 1y的多態lambda表達式。 – dyp
@dyp:爲什麼使用我的'FunctionTraits'來獲取單個參數的類型是危險的? –
第二種類型模板參數是多餘的?編譯器應該如何知道'BindingType'類型應該是什麼類型,因此調用'TBindingExpression'時會調用什麼重載? – rubenvb