eran
eran

Reputation: 6931

Compile error using boost::concept_check

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

Upvotes: 1

Views: 1649

Answers (2)

eran
eran

Reputation: 6931

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

Upvotes: 2

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 248289

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.

Upvotes: 3

Related Questions