[boost] [string_ref] Don't rely on implicit conversation for operators

88 views
Skip to first unread message

Nicola Bizzoca

unread,
May 17, 2013, 2:39:03 PM5/17/13
to bo...@lists.boost.org
Hi,
This is a follow up of "[string_ref] Add an equality operator between
string_ref and char*"

I'm sorry for the previous message, I'm started learning C++ and I'm making
some errors


Here the new topic:
boost::string_ref rely on implicit conversation for using operators

For example this operator:
template<typename charT, typename traits>
bool operator==(basic_string_ref<charT, traits> x, basic_string_ref<charT,
traits> y)

Is bad because if I write this:
boost::string_ref("http") == "http"

The compiler complains:
error: no match for ‘operator==’ (operand types are
‘boost::basic_string_ref<char, std::char_traits<char> >’ and ‘const char
[5]’)

It can't find the correct overload because implicit conversion is
considered after candidate templates have been considered

boost::string_ref should offer overloads for string and const char* so it
can be possible write code like
boost::string_ref("boost") == "boost" && boost::string_ref("c++") ==
std::string("c++")

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

Daniel James

unread,
May 19, 2013, 1:45:15 AM5/19/13
to bo...@lists.boost.org
On 17 May 2013 19:39, Nicola Bizzoca <nicola....@gmail.com> wrote:
>
> boost::string_ref should offer overloads for string and const char* so it
> can be possible write code like
> boost::string_ref("boost") == "boost" && boost::string_ref("c++") ==
> std::string("c++")

Thanks for the suggestion. It looks like this is coming in the next
release. There will be a beta release in early June if you want to try
that.

Daniel

Marshall Clow

unread,
May 19, 2013, 8:01:15 PM5/19/13
to bo...@lists.boost.org
On May 18, 2013, at 10:45 PM, Daniel James <dan...@calamity.org.uk> wrote:

> On 17 May 2013 19:39, Nicola Bizzoca <nicola....@gmail.com> wrote:
>>
>> boost::string_ref should offer overloads for string and const char* so it
>> can be possible write code like
>> boost::string_ref("boost") == "boost" && boost::string_ref("c++") ==
>> std::string("c++")
>
> Thanks for the suggestion. It looks like this is coming in the next
> release. There will be a beta release in early June if you want to try
> that.

Yes; there's code in there now to enable this (replacing other code that supposedly did that, but didn't work on MSVC).

-- Marshall

Marshall Clow Idio Software <mailto:mclow...@gmail.com>

A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait).
-- Yu Suzuki
Reply all
Reply to author
Forward
0 new messages