是合法的有class C : public A : public D {}
從來沒有看到過和gcc沒有編譯。如使用 發佈的代碼註釋,如果使用編譯條件(SFINAE)或簡單地使用超載。在GCC的實現是這樣的:
template<typename _Res, typename... _ArgTypes>
class function<_Res(_ArgTypes...)>
: public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>
... code continues here ...
其中_Maybe_unary_or_binary_function超載這樣的:
/**
* Derives from @c unary_function or @c binary_function, or perhaps
* nothing, depending on the number of arguments provided. The
* primary template is the basis case, which derives nothing.
*/
template<typename _Res, typename... _ArgTypes>
struct _Maybe_unary_or_binary_function { };
/// Derives from @c unary_function, as appropriate.
template<typename _Res, typename _T1>
struct _Maybe_unary_or_binary_function<_Res, _T1>
: std::unary_function<_T1, _Res> { };
/// Derives from @c binary_function, as appropriate.
template<typename _Res, typename _T1, typename _T2>
struct _Maybe_unary_or_binary_function<_Res, _T1, _T2>
: std::binary_function<_T1, _T2, _Res> { };
這
template<class A, B>
class foo<A(B)> //Can we have "<...>" here? What's its meaning?
{...}
它是常用的,如果你想什麼呢用函數語法編寫模板實例化,如:
foo< void(int) > f;
是的,你可以在任何地方使用...
:
template <typename RETVAL, typename ... PARMS>
class Func<RETVAL(PARMS...)>
{
};
int main()
{
Func<void(int, float)> f;
Func<int()> f1;
Func<float(std::string)> f2;
}
更新:
我認爲,如果A(B)是一個有效的函數類型,那麼它應該是
template<F>
class foo {...}
它不是function type
本身,它只是一種使用函數語法來定義模板參數的方法/語法。如果您將F
作爲函數類型使用,您可以將其設置爲模板模板。但是這對任何事情都沒有幫助,因爲在使用該類型的類中,必須訪問定義的每個參數。如果您在此處只有F
,則只能將此F
轉發給其他模板,因爲您無法在模板類本身內訪問返回值和參數F
。那有什麼好處呢?
UPDATE:
如果你想實現一個模板,這需要一個模板參數,它本身需要一個函數的參數,你可以做到這一點。我列出了來自普通模板,模板模板的所有變體以及所有這些也與函數類型。希望有幫助: //定義一個標準模板類 template class Templ {};
// define one with template template parameter
template < template <typename> class TEMPL >
class TemplTempl {};
// and now to catch a function type
template <typename RETVAL, typename ... PARMS> class Func;
template <typename RETVAL, typename ... PARMS>
class Func<RETVAL(PARMS...)>
{
public:
static void Do() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
};
// and how to define a template which takes a template which takes a function type parameter?
template < template < typename RETVAL, typename ... PARMS > class F>
class FuncTempl{};
template < template < typename , typename ... > class F, typename RETVAL, typename ... PARMS >
class FuncTemplUser
{
static void Do() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
};
template < template < typename , typename ... > class F, typename RETVAL, typename ... PARMS >
class FuncTemplUser< F, RETVAL(PARMS...)> : public F<RETVAL(PARMS...)>
{
public:
static void Do() { std::cout << __PRETTY_FUNCTION__ << std::endl; F<RETVAL(PARMS...)>::Do(); }
};
int main()
{
// using the standard templates
Templ<int> n1;
// and the template which takes a template as parm
TemplTempl<Templ> n2;
// now use one with function type
Func<void(int, float)> f;
Func<void(int, float)>::Do();
FuncTempl<Func> f2;
FuncTemplUser<Func, void(int)> f3;
f3.Do();
}
正如你可以看到FuncTempl
正在本身以後可以使用函數類型的模板。這正是你認爲你的F
將是。
你指的是libC++代碼嗎?我相信[您找到的代碼](https://github.com/llvm-mirror/libcxx/blob/master/include/functional)位於註釋塊內,並且實際上是僞代碼。 –
那時候我們不得不去調試評論... – bolov
@ChrisDrew OMG!它是!謝謝。但是,我仍然有第二個問題。 – Robert