English 中文(简体)
C++确定等级是否可比
原标题:C++ determine if class is comparable

我 m越多,越少越野,这也许是一个 st问题,但我没有设法找到任何简单的解决办法。

在C++中,我有这样的一类:

template<class T> class Node {...}

而我需要的是T,至少要有=和提;以及界定操作者。 是否有任何<>简单明了<>方法? Java就是这样:

public class Node<T extends Comparable> { ... }

感谢你们的帮助!

最佳回答

C++模板是duck-typed,因此没有必要进行接口或限制,汇编者如果存在,将使用比较操作者,并产生错误。

另见,详见

问题回答

如果你想避免加密错误(因为你经常在模版瞬时树中发生缺乏可比性的情况),那么,只要使用,就能够做到:

尤其是,在“增强力”的理论中,研究“增强模范类专业”:可推广。

http://www.boost.org/doc/libs/release/libs/type_traits/doc/html/index.html

对您的案件特别感兴趣:

http://www.boost.org/doc/libs/release/libs/type_traits/doc/html/boost_typetraits/vis:/has_ Equal_to.html”rel=“nofollow” http://www.boost.org/doc/libs/release/libs/type_traits/doc/html/boost_types/vis:/has_ Equal_to.html

http://www.boost.org/doc/libs/release/libs/type_traits/doc/html/boost_typetraits/reference/has_not_equal_to.html

But see also has_greater, has_greater_equal, has_less, has_less_equal, etc. // I m actually somewhat surprised that there isn t a straightforward is_equality_comparable type-trait.

// EDIT: it appears I ve found it, it s ::boost::is_equality_comparable::value in the Concept Traits Library: http://neoscientists.org/~tschwinger/boostdev/concept_traits/libs/concept_traits/doc/

然而,似乎被遗弃:

另一种解决办法是使用“夜总会”概念查询图书馆,特别是适用“平等委员会”概念:

rel=“nofollow”http://www.boost.org/doc/libs/release/libs/concept_check/using_concept_check.htm

还有另一种选择: Boost.Generic -- https://svn.boost.org/trac/boost/wiki/Libraries UnderConstruction#Boost.Generic

Prensentation:

Yet another alternative: http://code.google.com/p/origin/source/browse/trunk/core/tests/concepts/equality_comparable.cpp

目前在C++20概念图书馆是可能的。

class Node<T> requires Compare<T> {...}

虽然如果这一概念不满意,这仍会犯错误,但更容易理解造成错误的原因。 您可以在其周围写一个包裹,以预防错误。

如果贵模板类别使用你提到的操作人员,如果模板类型论点没有支持这些操作者,汇编者就会发现错误。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签