我将如何为以下结构编写模板约束
struct Foo (T, U) {
}
表示T
和 U
必须使用 <
进行比较?我的意思是两个 T
s 可以与 <
进行比较和两个 U
s 可以与 <
进行比较- 一个 T
和 U
可能是无与伦比的。
最佳答案
我相信这会满足您的要求,尽管可能有更简洁的解决方案:
struct Foo (T, U) if (is(typeof(T.init < T.init) : bool)
&& is(typeof(U.init < U.init) : bool)
{ }
你可以用模板稍微清理一下:
enum bool isSelfComparable(T) = is(typeof(T.init < T.init) : bool);
struct Foo (T, U) if (isSelfComparable!T && isSelfComparable!U) { }
关于templates - D:显示给定类型是否具有可比性的模板约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27282568/