Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ambigous overloaded operator==(std::string, char*) #285

Closed
sgn opened this issue Apr 26, 2023 · 1 comment
Closed

Ambigous overloaded operator==(std::string, char*) #285

sgn opened this issue Apr 26, 2023 · 1 comment

Comments

@sgn
Copy link

sgn commented Apr 26, 2023

Not sure if I should this issue should be reported here or reported to the other upstream, but this short example is failed to be compiled:

#include <string>
#include <boost/filesystem.hpp>

using namespace boost::filesystem;

bool f(char *arg) {
	return std::string("a-str") == arg;
}

GNU's g++ reports:

test.cpp: In function 'bool f(char*)':
test.cpp:7:40: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
7 | return std::string("a-str") == arg;
| ^~~
In file included from /usr/include/c++/12.2/string:53,
from test.cpp:1:
/usr/include/c++/12.2/bits/basic_string.h:3599:5: note: candidate 1: 'bool std::operator==(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const _CharT*) [with _CharT = char; _Traits = char_traits; _Alloc = allocator]'
3599 | operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
| ^~~~~~~~
In file included from /usr/include/boost/filesystem.hpp:16,
from test.cpp:2:
/usr/include/boost/filesystem/path.hpp:1294:9: note: candidate 2: 'typename boost::enable_if_c<boost::filesystem::detail::path_traits::is_convertible_to_path_source<typename boost::remove_cv::type>::value, bool>::type boost::filesystem::operator==(const path&, const Source&) [with Source = char*; typename boost::enable_if_c<detail::path_traits::is_convertible_to_path_source<typename boost::remove_cv::type>::value, bool>::type = bool; typename boost::remove_cv::type = char*]'
1294 | >::type operator==(path const& lhs, Source const& rhs)
| ^~~~~~~~

clang++ reports:

test.cpp:7:30: error: use of overloaded operator '==' is ambiguous (with operand types 'std::string' (aka 'basic_string') and 'char *')
return std::string("a-str") == arg;
~~~~~~~~~~~~~~~~~~~~ ^ ~~~
/usr/bin/../lib64/gcc/x86_64-linux-gnu/12.2/../../../../include/c++/12.2/bits/basic_string.h:3599:5: note: candidate function [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator]
operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/include/boost/filesystem/path.hpp:1294:9: note: candidate function [with Source = char *]

::type operator==(path const& lhs, Source const& rhs)
^
/usr/include/boost/filesystem/path.hpp:1303:9: note: candidate function [with Source = std::basic_string]
::type operator==(Source const& lhs, path const& rhs)
^
1 error generated.

@Lastique
Copy link
Member

I think this should work if you remove using namespace boost::filesystem;.

Lastique added a commit that referenced this issue May 7, 2023
…std lib.

Path comparison operators that accept arbitrary path source types now require
the other argument to be exactly path. This prevents the compiler from picking
those operators when the other argument is convertible to path. This can happen
even when neither of the arguments are actually paths, e.g. when the
comparison operators are brought into the current scope by a using directive.

Fixes #285.
Lastique added a commit that referenced this issue May 7, 2023
…std lib.

Path comparison operators that accept arbitrary path source types now require
the other argument to be exactly path. This prevents the compiler from picking
those operators when the other argument is convertible to path. This can happen
even when neither of the arguments are actually paths, e.g. when the
comparison operators are brought into the current scope by a using directive.

Fixes #285.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants