背景:我创建了以下类C,其构造函数应该采用N类型的变量B&:
class A;
class B
{
A* getA();
};
template<size_t N>
class C
{
public:
template<typename... Args>
inline C(Args&... args) :
member{args.getA()...}
{}
private:
std::array<A*, N> member;
};
Run Code Online (Sandbox Code Playgroud)
问题:我的问题是如何将variadic约束Args为所有类型B?
我的部分解决方案:我想定义一个谓词,如:
template <typename T, size_t N, typename... Args>
struct is_range_of :
std::true_type // if Args is N copies of T
std::false_type // otherwise
{};
Run Code Online (Sandbox Code Playgroud)
并相应地重新定义我的构造函数:
template <typename... Args,
typename = typename std::enable_if<is_range_of_<B, N, Args...>::value>::type
>
inline C(Args&... args); …Run Code Online (Sandbox Code Playgroud) 这个问题类似于这一个,但我要声明一个递归函数对象,而不是递归模块。所以我有 :
一个界面A:
module type A = sig
type t
val basic_func: ...
val complex_func: ...
end
Run Code Online (Sandbox Code Playgroud)
在以下方面ComplexImpl实现的函子:A.complex_funcA.basic_func
module ComplexImpl (SomeA : A) =
struct
let complex_impl params =
SomeA.basic_func ...
...
end
Run Code Online (Sandbox Code Playgroud)
另一个界面I:
module type I = sig
type t
...
end
Run Code Online (Sandbox Code Playgroud)
以及一个B接受类型参数的函子I,实现接口A并用于ComplexImpl实现complex_func。我想写这样的东西:
(* I can't write 'rec' here *)
module rec B (SomeI : I) :
A with …Run Code Online (Sandbox Code Playgroud) 我想定义以下模块层次结构,但它不起作用:
module type A = sig
type t
end
module type B = sig
type u
include A
end
module type C = sig
(* Error: Unbound type constructor u *)
include B with type t = u list
end
Run Code Online (Sandbox Code Playgroud)
为什么类型有错误u?
我已经定义了一个A由几个函子使用的接口,特别是MyFunctor:
module type A = sig
val basic_func: ...
val complex_func: ...
end
module MyFunctor :
functor (SomeA : A) ->
struct
...
let complex_impl params =
...
(* Here I call 'basic_func' from SomeA *)
SomeA.basic_func ...
...
end
Run Code Online (Sandbox Code Playgroud)
现在我想定义一个B实现接口的模块A。特别是, 的实现complex_func应该使用basic_functhrough complex_implin MyFunctor:
module B = struct
let basic_func = ...
let complex_func ... =
let module Impl = MyFunctor(B) in
Impl.complex_impl ...
end
Run Code Online (Sandbox Code Playgroud)
但是,此代码无法编译,因为 …