tags:

views:

219

answers:

2

Hi,

I'm trying to compile simple example to use the boost concept_check

Code is as follow:

#include <vector>
#include <complex>
#include <algorithm>
#include <boost/iterator.hpp>
#include <boost/concept_check.hpp>

template <class foo> 
void my_do_sort(std::vector<foo>& v) 
{
  BOOST_CONCEPT_ASSERT((RandomAccessIterator<foo>));
  std::stable_sort(v.begin(),v.end())
}
int main()
{
  std::vector<std::complex<double> > v;
  v.push_back(std::complex<double>(1,3));
  v.push_back(std::complex<double>(2,4));
  my_do_sort(v);
}

I then get the following error:

g++ -I~/tmp/BOOST/boost_1_39_0 -g3 -ggdb -pedantic -pedantic-errors -Wall -Werror -O0 --save-temps con1.cpp -o con1  
con1.cpp: In function 'void my_do_sort(std::vector<foo, std::allocator<_CharT> >&)':
con1.cpp:11: error: `*' cannot appear in a constant-expression
con1.cpp:11: error: a call to a constructor cannot appear in a constant-expression
con1.cpp:11: error: template argument 1 is invalid
con1.cpp:11: error: template argument 1 is invalid
con1.cpp:11: error: invalid type in declaration before ';' token
make: *** [con1] Error 1

Thanks

+2  A: 

If you re-read your code, this shouldn't be surprising. It fails to compile because the concept check fails. You are asserting that foo should implement the RandomAccessIterator concept. The entire point in the library is to produce a compile error (just like the one you're seeing) if the concept check fails.

But foo is not an iterator. it is a std::complex<double>.

It should be BOOST_CONCEPT_ASSERT((RandomAccessIterator<v::iterator>)); as far as I can see.

You want to check that the vector iterator is a random access iterator. Not that the complex numbers stored in the iterator are random access iterators.

jalf
Sorry, I did error in the example. However, I get this failure even with much simpler code, like:class MyClass {};void my_do_sort(MyClass c) { BOOST_CONCEPT_ASSERT((LessThanComparable<MyClass>));}
eran
That's why you should always copy/paste your code examples. If we can't trust the code posted to actually be the same as the code you're asking about, it's impossible to answer your question. Edit your question and add the corrected code.
jalf
+1  A: 

OK. This was just compilation issue. I had to use boost namespace.

Thanks

eran
I, for one, have been much helped by this!
redmoskito