是合法的有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