Конкатенация const_string - PullRequest
       23

Конкатенация const_string

0 голосов
/ 29 апреля 2011

Как мне объединить два const_string? Его дом http://conststring.sourceforge.net/ говорит цитата:

Он также использует шаблоны выражений для конкатенации, эффективно устраняя накладные расходы, возникающие в результате создания промежуточных временных объектов.

Итак, я подумал, что это самая простая форма

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

должно работать. Но gcc-4.6 жалуется на оператора +. Зачем? Должен ли я разыграть a и b до std::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

Ответы [ 2 ]

5 голосов
/ 29 апреля 2011

Вы включили соответствующий заголовок, boost/const_string/concatenation.hpp?

0 голосов
/ 29 апреля 2011

Класс 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+, кстати.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...