Background
Discussion about this was started under this answer for quite simple question.
Problem
This simple code has unexpected overload resolution of constructor for std::basic_string
:
#include <string>
int main() {
std::string s{"some string to test", 2, 3};
return 0;
}
Now someone was expecting that this will invoke this constructor:
std::basic_string<CharT,Traits,Allocator>::basic_string - cppreference.com
template< class T > basic_string( const T& t, size_type pos, size_type n, const Allocator& alloc = Allocator() ); (since C++17) (11) template< class T > constexpr basic_string( const T& t, size_type pos, size_type const Allocator& alloc = Allocator() ); (since C++20) (11)
Rationale is based on section of this c++ standard:
template<class T> constexpr basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator());
5 Constraints: is_convertible_v<const T&, basic_string_view<charT, traits>> is true.
6 Effects: Creates a variable, sv, as if by basic_string_view<charT, traits> sv = t; and then behaves the same as: basic_string(sv.substr(pos, n), a);
Now when this code is processed by cppinsights result is different then expected:
#include <string>
int main()
{
std::string s = std::basic_string<char>{std::basic_string<char>("some string to test", std::allocator<char>()), 2, 3};
return 0;
}
After collapsing this code to respective type definitions of std::string
it becomes this:
#include <string>
int main()
{
std::string s = std::string{std::string("some string to test"), 2, 3};
return 0;
}
So conversion of string literal to std::string
was performed and then constructor in form (3) was used. So undesired extra allocation is performed.
I'm sure this is not fault of tool I did other experiments to confirm that. Here is a godbolt example. Assembly clearly follows this pattern.
Analysis
To find explanation of this problem I've introduced an error to this code, so error report prints possible overload resolutions. Here is most interesting part of error report:
/opt/compiler-explorer/gcc-trunk-20220104/include/c++/12.0.0/bits/basic_string.h:771:9: note: candidate: 'template<class _Tp, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Tp&, size_type, size_type, const _Alloc&) [with <template-parameter-2-2> = _Tp; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
771 | basic_string(const _Tp& __t, size_type __pos, size_type __n,
| ^~~~~~~~~~~~
/opt/compiler-explorer/gcc-trunk-20220104/include/c++/12.0.0/bits/basic_string.h:771:9: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-trunk-20220104/include/c++/12.0.0/bits/char_traits.h:42,
from /opt/compiler-explorer/gcc-trunk-20220104/include/c++/12.0.0/string:40,
from <source>:1:
/opt/compiler-explorer/gcc-trunk-20220104/include/c++/12.0.0/type_traits: In substitution of 'template<bool _Cond, class _Tp> using enable_if_t = typename std::enable_if::type [with bool _Cond = false; _Tp = void]':
/opt/compiler-explorer/gcc-trunk-20220104/include/c++/12.0.0/bits/basic_string.h:156:8: required by substitution of 'template<class _CharT, class _Traits, class _Alloc> template<class _Tp, class _Res> using _If_sv = std::enable_if_t<std::__and_<std::is_convertible<const _Tp&, std::basic_string_view<_CharT, _Traits> >, std::__not_<std::is_convertible<const _Tp*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>*> >, std::__not_<std::is_convertible<const _Tp&, const _CharT*> > >::value, _Res> [with _Tp = char [20]; _Res = void; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
/opt/compiler-explorer/gcc-trunk-20220104/include/c++/12.0.0/bits/basic_string.h:769:30: required from here
/opt/compiler-explorer/gcc-trunk-20220104/include/c++/12.0.0/type_traits:2614:11: error: no type named 'type' in 'struct std::enable_if<false, void>'
2614 | using enable_if_t = typename enable_if<_Cond, _Tp>::type;
| ^~~~~~~~~~~
So overload (11) has been rejected by SFINAE.
Related standard library code looks like this: Overload (11) of std::basic_string constructor:
template<typename _Tp, typename = _If_sv<_Tp, void>>
basic_string(const _Tp& __t, size_type __pos, size_type __n,
const _Alloc& __a = _Alloc())
: basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
And here is implementation of _If_sv:
template<typename _Tp, typename _Res>
using _If_sv = enable_if_t<
__and_<is_convertible<const _Tp&, __sv_type>,
__not_<is_convertible<const _Tp*, const basic_string*>>,
__not_<is_convertible<const _Tp&, const _CharT*>>>::value,
_Res>;
So last part of this condition: __not_<is_convertible<const _Tp&, const _CharT*>>>::value
is a problem since clearly char array (string literal) is convertible to pointer to characters.
Question
Why does the implementation of _If_sv
look like this? What is the rationale of having this extra condition which is not mentioned in the standard?
If I understand overload resolution correctly, this extra condition is obsolete, since when first argument is std::string
overloads (3) has priority over overload (11) as exact match of non template function. On the other hand if an argument is something other than std::string
and it is convertible to std::string_view
then template overload (11) should win and extra allocation is avoided.
Is this a bug in the implementation of the standard library or did I miss something?
Why are these extra SFINAE conditions needed?
Bonus question
Is there a nice way to write a test which detects this issue?
Some ways to verify that a proper overload has been selected without modifying tested code (in this case std::basic_string
)?