Topic: C++0x concepts: Recursive constraints


Author: SG <s.gesemann@gmail.com>
Date: Sat, 18 Apr 2009 10:29:01 CST
Raw View
Hi!

Is anyone aware of whether the following code is supposed to compile?

   auto concept HasFoo<typename T> {
     void foo(T);
   }

   template<typename T>
     requires HasFoo<T>
   void foo(T const&) {}

   int main() {
     foo(42);
   }

It does compile using "conceptg++" (svn 727).  I'm not sure whether
this is really a good behaviour.  I came across this because I tried
to write a general function template that enables swapping lvalue with
rvalue without the need for any further specialized overloads:

   template<typename T>
     requires MoveConstructible<T> && MoveAssignable<T>
   void swap(T & a, T & b) { // <-- accepting lvalues only
     T temp ( move(a) );
     a = move(b);
     b = move(temp);
   }

   template<typename T, typename U>
     requires !SameType<T&&,U&&> // avoids ambiguity
           &&  HasSwap <T&, U&>  // for forwarding
   inline void swap(T && t, U && u)
   {
     swap(t,u); // forwarding to lvalue-swap
   }

I realized that in a rare combination of types T and U this could lead
to an infinite recursion via HasSwap<T&,U&>::swap.

I think this "recursive constraint" situation deserves some attention
and that it makes more sense to ignore the current function template
(that's being defined) as a function that satisfies any requirements
mentioned in its requires clauses. This would make the first example
ill-formed.


Cheers!
SG

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]