Как использовать MFC CString с библиотекой алгоритма строки повышения

Предварительное примечание: string_algo отлично работает с std::wstring, и, конечно, я могу (и делаю) сначала преобразовать объект(ы) CString в std::wstring, если и когда мне понадобится алгоритм из string_algo. Было бы очень хорошо, если бы я мог просто добавить объекты CString — интеграция с существующим кодом была бы намного проще!

Что я хочу сделать:

CString in = ...;
const CString out = boost::replace_last_copy(in, L"SEARCH", L"REPLACE");

Как предложено здесь, я попытался дополнительно включить boost/range/mfc.hpp, чтобы иметь Boost.Range адаптирован для MFC. (Хотя я не совсем понимаю этот заголовок глядя на него, кажется, что он ничего не делает для CString, только для классов коллекций.)

Boost 1.44.0 с Visual Studio 2005

Код теста выглядит так:

#include "stdafx.h"
#include <iostream>
#include <boost/range/mfc.hpp>
#include <boost/algorithm/string.hpp>

void f(CString const& input) {
    CString out = boost::replace_last_copy(input, L"FROM", L"TO");
    std::wcout << out << "\n";
}

Ошибка, которую я получаю, выглядит так:

1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format_store.hpp(77) : error C2039: 'type' : is not a member of 'boost::range_const_iterator<C>'
1>        with
1>        [
1>            C=const CString
1>        ]
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(139) : see reference to function template instantiation 'bool boost::algorithm::detail::check_find_result<const InputT,const FindResultT>(InputT &,FindResultT &)' being compiled
1>        with
1>        [
1>            InputT=CString,
1>            FindResultT=boost::iterator_range<const wchar_t *>
1>        ]
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\find_format.hpp(113) : see reference to function template instantiation 'InputT boost::algorithm::detail::find_format_copy_impl<SequenceT,FormatterT,boost::iterator_range<IteratorT>>(const InputT &,FormatterT,const FindResultT &)' being compiled
1>        with
1>        [
1>            InputT=CString,
1>            SequenceT=CString,
1>            FormatterT=boost::algorithm::detail::const_formatF<boost::iterator_range<const wchar_t *>>,
1>            IteratorT=const wchar_t *,
1>            FindResultT=boost::iterator_range<const wchar_t *>
1>        ]
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\replace.hpp(314) : see reference to function template instantiation 'SequenceT boost::algorithm::find_format_copy<SequenceT,boost::algorithm::detail::last_finderF<SearchIteratorT,PredicateT>,boost::algorithm::detail::const_formatF<RangeT>>(const SequenceT &,FinderT,FormatterT)' being compiled
1>        with
1>        [
1>            SequenceT=CString,
1>            SearchIteratorT=const wchar_t *,
1>            PredicateT=boost::algorithm::is_equal,
1>            RangeT=boost::iterator_range<const wchar_t *>,
1>            FinderT=boost::algorithm::detail::last_finderF<const wchar_t *,boost::algorithm::is_equal>,
1>            FormatterT=boost::algorithm::detail::const_formatF<boost::iterator_range<const wchar_t *>>
1>        ]
1>        ....\mytest.cpp(10) : see reference to function template instantiation 'SequenceT boost::algorithm::replace_last_copy<CString,const wchar_t[5],const wchar_t[3]>(const SequenceT &,Range1T (&),Range2T (&))' being compiled
1>        with
1>        [
1>            SequenceT=CString,
1>            Range1T=const wchar_t [5],
1>            Range2T=const wchar_t [3]
1>        ]
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format_store.hpp(78) : error C3203: 'type' : unspecialized class template can't be used as a template argument for template parameter 'IteratorT', expected a real type
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(121) : error C2780: 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,const InsertT &)' : expects 3 arguments - 4 provided
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\sequence.hpp(39) : see declaration of 'boost::algorithm::detail::insert'
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(144) : see reference to function template instantiation 'InputT boost::algorithm::detail::find_format_copy_impl2<InputT,FormatterT,FindResultT,boost::iterator_range<IteratorT>>(const InputT &,FormatterT,const FindResultT &,const FormatResultT &)' being compiled
1>        with
1>        [
1>            InputT=CString,
1>            FormatterT=boost::algorithm::detail::const_formatF<boost::iterator_range<const wchar_t *>>,
1>            FindResultT=boost::iterator_range<const wchar_t *>,
1>            IteratorT=const wchar_t *,
1>            FormatResultT=boost::iterator_range<const wchar_t *>
1>        ]
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(121) : error C2893: Failed to specialize function template 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,ForwardIteratorT,ForwardIteratorT)'
1>        With the following template arguments:
1>        'CString'
1>        'const wchar_t *'
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(123) : error C2893: Failed to specialize function template 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,const InsertT &)'
1>        With the following template arguments:
1>        'CString'
1>        'boost::iterator_range<IteratorT>'
1>        with
1>        [
1>            IteratorT=const wchar_t *
1>        ]
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(123) : error C2780: 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,ForwardIteratorT,ForwardIteratorT)' : expects 4 arguments - 3 provided
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\sequence.hpp(29) : see declaration of 'boost::algorithm::detail::insert'
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(125) : error C2780: 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,const InsertT &)' : expects 3 arguments - 4 provided
1>        c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\sequence.hpp(39) : see declaration of 'boost::algorithm::detail::insert'
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(125) : error C2893: Failed to specialize function template 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,ForwardIteratorT,ForwardIteratorT)'
1>        With the following template arguments:
1>        'CString'
1>        'const wchar_t *'

person Martin Ba    schedule 04.07.2013    source источник


Ответы (1)


Попробуйте заменить CString на CStringW, если CString сопоставляется с CStringA.

Как насчет точного кода ошибки, в какой строке заголовка? Можете ли вы попробовать заменить ту же ошибку в локальном коде?

person Ajay    schedule 25.10.2013
comment
Это определенно была сборка UNICODE, так что CStringW. Полный тестовый код находится в вопросе. (по модулю в основном пустой main) - person Martin Ba; 28.10.2013
comment
Что, если вы явно приведете тип к LPCWSTR, или используете GetString, или передадите неконстантный объект CString? - person Ajay; 28.10.2013