Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2007-07-22 10:38:37


On Sun, July 22, 2007 16:26, Ovanes Markarian wrote:

>
> Hi Martin,
>
> if you simply would like to ensure that you compile a correct version, I would suggest the
> following solution:
>
>
> A template function which compares a type instance with itself (makes no sense during the runtime
> since is always true, if there is a comparison operator, otherwise a compiler error is issued)
>
> template<class Type_>
> inline void concept_comparison_exists(Type_ const& t)
> {
> static_cast<bool>(t==t);
> }
>
> static_cast<bool>(t==t) is used to notify that compiler that this is an intended code and no
> warning should be produced.
>
>
> Now some tests:
>
> class SomeClassX //empty class to ensure that out concept works
> {};
>
>
> int main(int argc, char* argv[])
> {
>
> int i = 8;
>
> concept_comparison_exists(i);
> concept_comparison_exists(SomeClassX());
>
> return 0;
> }

Just in addition to my post, if SomeClassX can be implicitly converted to some type which defines
the operator==, the code will still compile fine. Here an example:

class SomeClassX
{
  public:
     operator int()const
     {
         return 0;
     }
};

this line will produce NO error, since SomeClassX will be implicitly casted to int and int has the
comparison operator.
concept_comparison_exists(SomeClassX());

But in such a case you can't anyway assume what was the intention of the developer. So I would let
it be valid.

With Kind Regards,

Ovanes


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net