編譯器如何知道正確的類型,這樣的代碼:模板扣和函數指針
class Base
{
protected:
typedef View * ViewType;
typedef boost::function<ViewType()> ActionType;
typedef boost::unordered_map<std::string, ActionType> ActionMapType;
ActionMapType actions;
template <class ControllerType>
inline void addAction(std::string actionName, ViewType (ControllerType::*action)()) { actions.insert(ActionMapType::value_type(actionName, bind<ViewType>(&action, static_cast<ControllerType *>(this)))); }
};
class Derived : public Base
{
Derived()
{
addAction("someAction", &Derived::foo); // No template
}
ViewType foo() { cout << "foo"; }
}
我知道,我傳遞推導ControllerType但是編譯器怎麼能知道肯定派生是模板參數?
我已經得到了。我在問爲什麼會發生。看到布魯斯的回答。 – 2010-01-15 02:22:27
你的意思是,你知道模板參數演繹如何工作,但你不知道模板參數演繹存在嗎? – AnT 2010-01-15 02:24:15
我知道模板演繹可能會發生,我實際上並不知道什麼時候。當它工作時,我猜想它與函數指針有關,但我不確定。此外,我不知道存在模板參數推理。 – 2010-01-15 02:37:57