2012-04-28 46 views

回答

4

你可以使用tag分派

template <typename T> class xyz 
{ 
    public: 
    void foo() 
    { 
    foo(typename boost::is_base_of<bar,T>::type()); 
    } 

    protected: 
    void foo(boost::mpl::true_ const&) 
    { 
    // Something 
    } 

    void foo(boost::mpl::false_ const&) 
    { 
    // Some other thing 
    } 
}; 

注意標籤調度通常比使用enable_if SFINAE更好,因爲enable_if需要的線性數選擇合適的載之前模板實例。

在C++ 11,可以使用的std ::這些相當於提振元功能。