c++stringclangarm64libstdc++

What does this constructor basic_string<decltype(nullptr)>(char const*) do?


Recently i was reading some assembly codes of aarch64, and i found this strange constructor which seems not defined in the basic_string:

_ZNSt4__n112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEC2B6v15004IDnEEPKc

And the demangled name is:

std::__n1::basic_string<char, std::__n1::char_traits<char>, std::__n1::allocator<char> >::basic_string[abi:v15004]<decltype(nullptr)>(char const*)

And this function may do nothing on the given this pointer.

Because in the assembly i found the x8 register is used to store the pointer, and it is constructed by this function, and is destructed before ret.

But later in the caller, this x8 is used in operator=(std::string&, std::string&&), seems this will cause use after free error.

This looks strange, so could anyone tell me what does this function do? And is this function only generated by compiler? (means user cannot write this code)

If i try to use it directly, i will get this error:

context.cpp:11:61: error: cannot call constructor ‘std::__cxx11::basic_string<char>::basic_string’ directly [-fpermissive]
   11 |     std::basic_string<char>::basic_string<decltype(nullptr)>(name);
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~

Solution

  • It is an overload for std::string(nullptr), but note that this constructor is explicitly deleted starting in C++23

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

    basic_string( std::nullptr_t ) = delete;   //   (since C++23)