Learning concept checking the hard way - P3
March 25, 2014
In the previous posts this and this, we looked at the basics of concept checking and stuff you need to know.
Finally here’s our
Let’s dissect the above method and see what’s happening.
has_helper method has two possible overload options. For the
datatype both are possible overload candidates but as we saw earlier
compiler prefers the non-variadic option. So it choosses the first
option which has a return type of
complex<int>, compiler has only one option since
doesn’t exist for
false_type are just structs with values as
false resp. Check out their usage
We could directly use
has_less_helper to know what Type has a less
than operator defined for it. However it is important to note that the
has_less_helper assumes that type
T has a default constructor
defined for it(
decltype(T() < T())).
To remove above dependency we could use declvalue(as discussed in
previous post). That is what we are
constexpr keyword makes the method evaluate at compile time. So we will have a true or false value from this method at compile time.
Inside our definition of generic
BinarySearchTree we put a
static_assert(compile time assert) to check that we have a less that
operator define for the type T.
That’s it! Now when you declare something like
We get a compile time error.
Awesome! Now you understand the concept of concept checking. Happy coding.