static_assert_example_2.cpp 1021 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // (C) Copyright John Maddock 2000.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org for most recent version including documentation.
  6. #include <boost/static_assert.hpp>
  7. #include <boost/type_traits.hpp>
  8. #include <iterator>
  9. #include <list>
  10. #include <deque>
  11. template <class RandomAccessIterator >
  12. RandomAccessIterator foo(RandomAccessIterator from, RandomAccessIterator /*to*/)
  13. {
  14. // this template can only be used with
  15. // random access iterators...
  16. typedef typename std::iterator_traits< RandomAccessIterator >::iterator_category cat;
  17. BOOST_STATIC_ASSERT((boost::is_convertible<cat, const std::random_access_iterator_tag&>::value));
  18. //
  19. // detail goes here...
  20. return from;
  21. }
  22. int main()
  23. {
  24. std::deque<int> d;
  25. std::list<int> l;
  26. foo(d.begin(), d.end()); // OK
  27. //foo(l.begin(), l.end()); // error
  28. return 0;
  29. }