[boost] boost::trim_copy(boost::string_view()); // error

193 views
Skip to first unread message

Olaf van der Spek via Boost

unread,
Oct 31, 2021, 6:40:29 AM10/31/21
to boost, Olaf van der Spek
Hi,

Why does the overload for boost::string_view not work? The std one works.
Could it be made to work?

#include <boost/algorithm/string.hpp>
#include <boost/utility/string_ref.hpp>
#include <boost/utility/string_view.hpp>
#include <iostream>
#include <string_view>

int main()
{
boost::trim_copy(boost::string_view()); // error
boost::trim_copy(std::string_view());
}

$ g++ t0.cpp
In file included from /usr/include/boost/algorithm/string.hpp:19,
from t0.cpp:1:
/usr/include/boost/algorithm/string/trim.hpp: In instantiation of
‘SequenceT boost::algorithm::trim_copy_if(const SequenceT&,
PredicateT) [with SequenceT = boost::basic_string_view<char,
std::char_traits<char> >; PredicateT =
boost::algorithm::detail::is_classifiedF]’:
/usr/include/boost/algorithm/string/trim.hpp:343:49: required from
‘SequenceT boost::algorithm::trim_copy(const SequenceT&, const
std::locale&) [with SequenceT = boost::basic_string_view<char,
std::char_traits<char> >]’
t0.cpp:10:19: required from here
/usr/include/boost/algorithm/string/trim.hpp:319:20: error: no
matching function for call to ‘boost::basic_string_view<char,
std::char_traits<char> >::basic_string_view(const char*, const
char*&)’
319 | return SequenceT(
| ^~~~~~~~~~
320 | detail::trim_begin(
| ~~~~~~~~~~~~~~~~~~~
321 | ::boost::begin(Input),
| ~~~~~~~~~~~~~~~~~~~~~~
322 | TrimEnd,
| ~~~~~~~~
323 | IsSpace),
| ~~~~~~~~~
324 | TrimEnd
| ~~~~~~~
325 | );
| ~
In file included from t0.cpp:3:
/usr/include/boost/utility/string_view.hpp:97:9: note: candidate:
‘template<class Allocator> boost::basic_string_view<charT,
traits>::basic_string_view(const std::__cxx11::basic_string<charT,
traits, Allocator>&) [with Allocator = Allocator; charT = char; traits
= std::char_traits<char>]’
97 | basic_string_view(const std::basic_string<charT,
traits, Allocator>& str) BOOST_NOEXCEPT
| ^~~~~~~~~~~~~~~~~
/usr/include/boost/utility/string_view.hpp:97:9: note: template
argument deduction/substitution failed:
In file included from /usr/include/boost/algorithm/string.hpp:19,
from t0.cpp:1:
/usr/include/boost/algorithm/string/trim.hpp:319:20: note:
mismatched types ‘const std::__cxx11::basic_string<char,
std::char_traits<char>, Allocator>’ and ‘const char*’
319 | return SequenceT(
| ^~~~~~~~~~
320 | detail::trim_begin(
| ~~~~~~~~~~~~~~~~~~~
321 | ::boost::begin(Input),
| ~~~~~~~~~~~~~~~~~~~~~~
322 | TrimEnd,
| ~~~~~~~~
323 | IsSpace),
| ~~~~~~~~~
324 | TrimEnd
| ~~~~~~~
325 | );
| ~
In file included from t0.cpp:3:
/usr/include/boost/utility/string_view.hpp:110:23: note: candidate:
‘constexpr boost::basic_string_view<charT,
traits>::basic_string_view(const charT*,
boost::basic_string_view<charT, traits>::size_type) [with charT =
char; traits = std::char_traits<char>; boost::basic_string_view<charT,
traits>::size_type = long unsigned int]’ (near match)
110 | BOOST_CONSTEXPR basic_string_view(const charT* str, size_type len)
| ^~~~~~~~~~~~~~~~~
/usr/include/boost/utility/string_view.hpp:110:23: note: conversion
of argument 2 would be ill-formed:
In file included from /usr/include/boost/algorithm/string.hpp:19,
from t0.cpp:1:
/usr/include/boost/algorithm/string/trim.hpp:324:17: error: invalid
conversion from
‘boost::range_detail::extract_const_iterator<boost::basic_string_view<char,
std::char_traits<char> >, true>::typ’ {aka ‘const char*’} to
‘boost::basic_string_view<char, std::char_traits<char> >::size_type’
{aka ‘long unsigned int’} [-fpermissive]
324 | TrimEnd
| ^~~~~~~
| |
|
boost::range_detail::extract_const_iterator<boost::basic_string_view<char,
std::char_traits<char> >, true>::type {aka const char*}
In file included from t0.cpp:3:
/usr/include/boost/utility/string_view.hpp:107:23: note: candidate:
‘constexpr boost::basic_string_view<charT,
traits>::basic_string_view(const charT*) [with charT = char; traits =
std::char_traits<char>]’
107 | BOOST_CONSTEXPR basic_string_view(const charT* str)
| ^~~~~~~~~~~~~~~~~
/usr/include/boost/utility/string_view.hpp:107:23: note: candidate
expects 1 argument, 2 provided
/usr/include/boost/utility/string_view.hpp:78:23: note: candidate:
‘constexpr boost::basic_string_view<charT,
traits>::basic_string_view(const boost::basic_string_view<charT,
traits>&) [with charT = char; traits = std::char_traits<char>]’
78 | BOOST_CONSTEXPR basic_string_view(const
basic_string_view &rhs) BOOST_NOEXCEPT
| ^~~~~~~~~~~~~~~~~
/usr/include/boost/utility/string_view.hpp:78:23: note: candidate
expects 1 argument, 2 provided
/usr/include/boost/utility/string_view.hpp:73:23: note: candidate:
‘constexpr boost::basic_string_view<charT,
traits>::basic_string_view() [with charT = char; traits =
std::char_traits<char>]’
73 | BOOST_CONSTEXPR basic_string_view() BOOST_NOEXCEPT
| ^~~~~~~~~~~~~~~~~
/usr/include/boost/utility/string_view.hpp:73:23: note: candidate
expects 0 arguments, 2 provided
In file included from /usr/include/boost/algorithm/string.hpp:19,
from t0.cpp:1:
/usr/include/boost/algorithm/string/trim.hpp: In instantiation of
‘SequenceT boost::algorithm::trim_copy_if(const SequenceT&,
PredicateT) [with SequenceT = std::basic_string_view<char>; PredicateT
= boost::algorithm::detail::is_classifiedF]’:
/usr/include/boost/algorithm/string/trim.hpp:343:49: required from
‘SequenceT boost::algorithm::trim_copy(const SequenceT&, const
std::locale&) [with SequenceT = std::basic_string_view<char>]’
t0.cpp:11:19: required from here
/usr/include/boost/algorithm/string/trim.hpp:324:17: error: invalid
conversion from
‘boost::range_detail::extract_const_iterator<std::basic_string_view<char>,
true>::type’ {aka ‘const char*’} to
‘std::basic_string_view<char>::size_type’ {aka ‘long unsigned int’}
[-fpermissive]
324 | TrimEnd
| ^~~~~~~
| |
|
boost::range_detail::extract_const_iterator<std::basic_string_view<char>,
true>::type {aka const char*}
In file included from /usr/include/c++/11/bits/basic_string.h:48,
from /usr/include/c++/11/string:55,
from
/usr/include/boost/algorithm/string/std/string_traits.hpp:15,
from
/usr/include/boost/algorithm/string/std_containers_traits.hpp:19,
from /usr/include/boost/algorithm/string.hpp:18,
from t0.cpp:1:
/usr/include/c++/11/string_view:137:56: note: initializing argument
2 of ‘constexpr std::basic_string_view<_CharT,
_Traits>::basic_string_view(const _CharT*,
std::basic_string_view<_CharT, _Traits>::size_type) [with _CharT =
char; _Traits = std::char_traits<char>; std::basic_string_view<_CharT,
_Traits>::size_type = long unsigned int]’
137 | basic_string_view(const _CharT* __str, size_type __len) noexcept
| ~~~~~~~~~~^~~~~


--
Olaf

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Peter Dimov via Boost

unread,
Oct 31, 2021, 9:06:16 AM10/31/21
to bo...@lists.boost.org, Peter Dimov
Olaf van der Spek wrote:
> Hi,
>
> Why does the overload for boost::string_view not work? The std one works.
> Could it be made to work?
>
> #include <boost/algorithm/string.hpp>
> #include <boost/utility/string_ref.hpp>
> #include <boost/utility/string_view.hpp>
> #include <iostream>
> #include <string_view>
>
> int main()
> {
> boost::trim_copy(boost::string_view()); // error
> boost::trim_copy(std::string_view());
> }

std::string_view doesn't work either in C++17; it requires constructor (5) in

https://en.cppreference.com/w/cpp/string/basic_string_view/basic_string_view

that was added in C++20.

boost::string_view can be made to work by the addition of a constructor
that takes two pointers to char, like this

https://github.com/boostorg/core/blob/12f5f51427fbc9d27f56e5de002b0008fea420c9/include/boost/core/detail/string_view.hpp#L364-L368
Reply all
Reply to author
Forward
0 new messages