-5

A string literal is of type const char [N], and you can pass this type to a templated function that looks like:

template <uint64_t N>
void func(const char (&arg)[N]);

On cplusplus.com, there is no mention of such a templated constructor.

But, on cppreference.com, there is one that looks like this:

template<class StringViewLike>
explicit basic_string( const StringViewLike& t, Allocator& alloc = Allocator() );

I'm wondering if StringViewLike includes things like a const char array reference, like if you gave it a "string-literal", or does the "string-literal" argument to the constructor always use the const char*?

10
  • 3
    Side note: Don't look at cplusplus.com. It has an incredible amount of bad and wrong information. Just forget it exists. cppreference.com is much better; sure there are also the odd situation where it gets things wrong, but it is so much better and errors tend to get corrected fast (unlike that other site you mentioned). Commented 19 hours ago
  • 1
    I stopped using cplusplus when c++11 came, and I am not surprised to see that apparently they didnt even try to catch up any further Commented 19 hours ago
  • 1
    Possibly related: Was a new constructor added to std::string in c++23 that accepts std::array<char, N>? Commented 19 hours ago
  • 1
    Perhaps std::string stops at \0 would be enlightening. (A std::string constructed from "A\0B" stops at the embedded null character, unlike one constructed via "A\0B"s.) In fact, this answer to that question appears to address your question. Commented 18 hours ago
  • 1
    @Zebrafish the templated array overload is chosen since its an exact match. the char* overload requires a decay operation so it's less preferred. Commented 16 hours ago

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.