OpenPayments Developer Contest

How to require certain concepts in C++ code?

How do I require and check that an argument is a certain concept in C++?

For example, the random_shuffle function in the algorithm header requires that its arguments are RandomAccessIterators:

  template<typename _RandomAccessIterator>
    inline void
    random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
    {
      // concept requirements
      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
            _RandomAccessIterator>)
      __glibcxx_requires_valid_range(__first, __last);

      if (__first != __last)
        for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
          std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
    }

I guess I can't use these __glibcxx_function_requires etc. in my own code? How do they work? Do you check things like that in your code?



Bookmark and Share   Read more Read more...   Source: Stack Overflow