0

如何连接两个const_string的?它的家http://conststring.sourceforge.net/说引文:连接const_string的

它还使用表达模板进行连接,有效地消除了创建中间临时对象产生的开销。

所以我想最简单的形式

typedef class boost::const_string<char> csc; 
csc a, b; 
csc c = a+b; 

应该工作。但gcc-4.6抱怨operator +。为什么?我必须投abstd::string

错误输出如下:

semnet/realfile.cpp:185:13: error: no match for ‘operator+’ in ‘a + b’ 
semnet/realfile.cpp:185:13: note: candidates are: 
/usr/include/c++/4.6/bits/stl_bvector.h:352:3: note: std::_Bit_const_iterator std::operator+(std::ptrdiff_t, const std::_Bit_const_iterator&) 
/usr/include/c++/4.6/bits/stl_bvector.h:352:3: note: no known conversion for argument 1 from ‘csc’ to ‘std::ptrdiff_t’ 
/usr/include/c++/4.6/bits/stl_bvector.h:266:3: note: std::_Bit_iterator std::operator+(std::ptrdiff_t, const std::_Bit_iterator&) 
/usr/include/c++/4.6/bits/stl_bvector.h:266:3: note: no known conversion for argument 1 from ‘csc’ to ‘std::ptrdiff_t’ 
/usr/include/c++/4.6/bits/basic_string.h:2414:20: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::basic_string<_CharT, _Traits, _Alloc>&&, _CharT) 
/usr/include/c++/4.6/bits/basic_string.h:2408:27: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::basic_string<_CharT, _Traits, _Alloc>&&, const _CharT*) 
/usr/include/c++/4.6/bits/basic_string.h:2402:53: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, std::basic_string<_CharT, _Traits, _Alloc>&&) 
/usr/include/c++/4.6/bits/basic_string.h:2396:53: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>&&) 
/usr/include/c++/4.6/bits/basic_string.h:2384:53: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::basic_string<_CharT, _Traits, _Alloc>&&, std::basic_string<_CharT, _Traits, _Alloc>&&) 
/usr/include/c++/4.6/bits/basic_string.h:2378:53: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>&&) 
/usr/include/c++/4.6/bits/basic_string.h:2372:58: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::basic_string<_CharT, _Traits, _Alloc>&&, const std::basic_string<_CharT, _Traits, _Alloc>&) 
/usr/include/c++/4.6/bits/basic_string.h:2359:79: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, _CharT) 
/usr/include/c++/4.6/bits/basic_string.h:2344:26: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) 
/usr/include/c++/4.6/bits/basic_string.tcc:710:79: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::basic_string<_CharT, _Traits, _Alloc>&) 
/usr/include/c++/4.6/bits/basic_string.tcc:695:58: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&) 
/usr/include/c++/4.6/bits/basic_string.h:2307:58: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&) 
/usr/include/c++/4.6/bits/stl_iterator.h:1113:43: note: template<class _Iterator> std::move_iterator<_Iterator> std::operator+(typename std::move_iterator<_Iterator>::difference_type, const std::move_iterator<_Iterator>&) 
/usr/include/c++/4.6/bits/stl_iterator.h:328:46: note: template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&) 
/usr/include/c++/4.6/bits/stl_iterator.h:897:64: note: template<class _Iterator, class _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::operator+(typename __gnu_cxx::__normal_iterator<_Iterator, _Container>::difference_type, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&) 
semnet/realfile.cpp:186:63: error: no match for ‘operator+’ in ‘xns + boost::const_string<char>(boost::cref [with T = const char*]((* & chashid_get_name_static(hid))), 4294967295u)’ 
semnet/realfile.cpp:186:63: note: candidates are: 
/usr/include/c++/4.6/bits/stl_bvector.h:352:3: note: std::_Bit_const_iterator std::operator+(std::ptrdiff_t, const std::_Bit_const_iterator&) 
/usr/include/c++/4.6/bits/stl_bvector.h:352:3: note: no known conversion for argument 1 from ‘const csc’ to ‘std::ptrdiff_t’ 
/usr/include/c++/4.6/bits/stl_bvector.h:266:3: note: std::_Bit_iterator std::operator+(std::ptrdiff_t, const std::_Bit_iterator&) 
/usr/include/c++/4.6/bits/stl_bvector.h:266:3: note: no known conversion for argument 1 from ‘const csc’ to ‘std::ptrdiff_t’ 
/usr/include/c++/4.6/bits/basic_string.h:2414:20: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::basic_string<_CharT, _Traits, _Alloc>&&, _CharT) 
/usr/include/c++/4.6/bits/basic_string.h:2408:27: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::basic_string<_CharT, _Traits, _Alloc>&&, const _CharT*) 
/usr/include/c++/4.6/bits/basic_string.h:2402:53: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, std::basic_string<_CharT, _Traits, _Alloc>&&) 
/usr/include/c++/4.6/bits/basic_string.h:2396:53: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>&&) 
/usr/include/c++/4.6/bits/basic_string.h:2384:53: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::basic_string<_CharT, _Traits, _Alloc>&&, std::basic_string<_CharT, _Traits, _Alloc>&&) 
/usr/include/c++/4.6/bits/basic_string.h:2378:53: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>&&) 
/usr/include/c++/4.6/bits/basic_string.h:2372:58: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(std::basic_string<_CharT, _Traits, _Alloc>&&, const std::basic_string<_CharT, _Traits, _Alloc>&) 
/usr/include/c++/4.6/bits/basic_string.h:2359:79: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, _CharT) 
/usr/include/c++/4.6/bits/basic_string.h:2344:26: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) 
/usr/include/c++/4.6/bits/basic_string.tcc:710:79: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::basic_string<_CharT, _Traits, _Alloc>&) 
/usr/include/c++/4.6/bits/basic_string.tcc:695:58: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&) 
/usr/include/c++/4.6/bits/basic_string.h:2307:58: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&) 
/usr/include/c++/4.6/bits/stl_iterator.h:1113:43: note: template<class _Iterator> std::move_iterator<_Iterator> std::operator+(typename std::move_iterator<_Iterator>::difference_type, const std::move_iterator<_Iterator>&) 
/usr/include/c++/4.6/bits/stl_iterator.h:328:46: note: template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&) 
/usr/include/c++/4.6/bits/stl_iterator.h:897:64: note: template<class _Iterator, class _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::operator+(typename __gnu_cxx::__normal_iterator<_Iterator, _Container>::difference_type, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&) 
semnet/realfile.cpp:187:1: warning: control reaches end of non-void function [-Wreturn-type] 
make: *** [semnet/realfile.o] Fel 1 

Compilation exited abnormally with code 2 at Fri Apr 29 15:42:00 

回答

5

是否包含相关的头,boost/const_string/concatenation.hpp

+0

这可能是一个评论:) – sehe 2011-04-29 14:00:11

+0

@sehe真的吗?这是答案,不是吗?我本可以把它作为一个答案而不是一个问题... – 2011-04-29 14:03:19

+0

哦,舒特。我错过了尾巴中的刺痛(concatenation.hpp):) – sehe 2011-04-29 14:04:23

0

const_string类包含用于级联构造:

template<class T, class U> 
const_string(concatenation<const_string, T, U> const& expr) // throw(std::bad_alloc, std::length_error) 
    : storage_type(0, expr.size()) 
{ 
    expr.copy_result_to(const_cast<char_type*>(this->begin())); 
} 

和库本身包含一个concatenation类有两个const_strings

你可以使用这些。

其中定义concatenation类的头也是其中operator+被定义的位置,顺便说一句。

+0

“你应该使用这些” - 不。表达式模板的要点是你*不需要直接使用那个类(除非你想存储中间结果)。你可以放心地使用'operator +'。 – 2011-04-29 14:01:18

+1

@Konrad s/should/could/g :) – rlc 2011-04-29 14:30:28