Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8904229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:04:30+00:00 2026-06-15T02:04:30+00:00

String literals are array objects: typeid(hello).name() // char [6] This seems convenient because the

  • 0

String literals are array objects:

typeid("hello").name()   // char [6]

This seems convenient because the size of the string literal (=array) is known at compile time. So why is there no constructor to std::string that takes a reference to an array?

// this would be great
template <int N>
std::string(const char (& array)[N]);

Instead there are constructors that take const char * s, const char * s, size_t n or two InputIterators (eg. const char * begin, const char * end). All of these have drawbacks; the array is implicitly converted to a pointer and the size information is lost, so various methods are used to get it back. Working, but increasingly crazy examples:

// std::string(const char * s) version:
std::string s1("hello");                      // calls std::strlen(s) internally

// std::string(const char * s, size_t n) version:
std::string s2("hello", 5);                   // programmer does std::strlen(s) mentally
std::string s3("hello", sizeof("hello"));     // have to repeat string literal

// macro helper to avoid repeating string literal (ugly and dangerous)
#define STRLIT(x) std::string(x, sizeof(x));  // could be a function I guess
std::string s4 = STRLIT("hello");             // not much improvement (and macros are evil)

// std::string(InputIterator begin, InputIterator end) version:
char tmp[] = "hello";                         // copy array
std::string s5(&tmp[0], &tmp[sizeof(tmp)]);   // so you can reference it twice

// or trust the compiler to return the same address for both literals
std::string s6(&"hello"[0], &"hello"[sizeof("hello")]);   // totally crazy
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:04:31+00:00Added an answer on June 15, 2026 at 2:04 am

    The key question when considering whether there should be a constructor taking a char const (&)[N] (for some statically determined N) is what the content of the resulting std::string should be. It might be obvious to some what it should be but I don’t think it is. Consider this std::string("abc\0def"):

    1. It could be a string with 8 elements, including the two null bytes.
    2. It could be a string with 7 elements, excluding the second null bytes.
    3. It could be a string with 3 elements, excluding everything starting at the first null byte.

    If you use

    std::cout << "string='" << "abc\0def" << "'\n";
    

    you get the third option. It seems, this is the only sane alternative…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is only in char *ptr = Hello World a string literal or are both
I want to be able to pass either a string literal, 'this is a
Possible Duplicate: Modifying C string constants? Pointer to const char vs char array vs
typedef struct unit_class_struct { char *name; char *last_name; } person; int setName(person *array) {
I have an array of string literals and I want to loop over them,
Clarification : Given that a string literal can be rewritten as a const char[]
In C11, a new string literal was added with the prefix u8 . This
I am interested in where string literals get allocated/stored. I did find one intriguing
Tomcat does not encode correctly String literals that contain unicode characters. The problem occurs
I'm trying to match chunks of JS code and extract string literals that contain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.