c++ - 使用boost::concept_check编译报错

标签 c++ boost

我正在尝试编译简单示例以使用 boost concept_check

代码如下:

#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);
}

然后我得到以下错误:

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

谢谢

最佳答案

如果您重新阅读您的代码,这应该不足为奇。它无法编译因为概念检查失败。你断言 foo应该实现 RandomAccessIterator 概念。如果概念检查失败,库中的全部要点就是产生一个编译错误(就像您看到的那样)。

但是foo不是迭代器。这是一个std::complex<double> .

应该是BOOST_CONCEPT_ASSERT((RandomAccessIterator<v::iterator>));据我所知。

您想检查 vector 迭代器是否为随机访问迭代器。并不是说存储在迭代器中的复数是随机访问迭代器。

关于c++ - 使用boost::concept_check编译报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1293708/

相关文章:

c++ - MySQL C++ 连接器使我的应用程序在 ResultSet->getString() 处崩溃

c++ - 是否对 boost 进行了优化以使用 C++11 中的可变参数模板?

c++ - Boost::tuple 相当于 Python 的 itemgetter?

c++ - 为什么 STL vector 不能包含协程对象?

c++ - 使用 Concepts Lite 为具有成员函数模板的类型指定概念

c++ - opengl中如何使用子窗口

c++ - X DevAPI mysqlx::Session() over linux socket 失败并显示 “CDK Error: unexpected message”

c++ - 如何初始化作为类成员的 shared_ptr?

c++ - Boost Graph Library 动态边权重

c++ - 魔鬼不加载图像与 linux 构建