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

  • Home
  • SEARCH
  • 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 9246619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:32:02+00:00 2026-06-18T09:32:02+00:00

#include <iostream> #include <string> using namespace std; int main() { wcout << LHello; //

  • 0
#include <iostream>
#include <string>

using namespace std;

int main()
{
    wcout << L"Hello";          // OK.
    wcout << wstring(L"Hello"); // OK.
    wcout << "Hello";           // OK. Why?
    wcout << string("Hello");   // Error. Why?
}

Why does std::wcout accept a narrow string literal as its argument but doesn’t accept a narrow string object?

  • 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-18T09:32:04+00:00Added an answer on June 18, 2026 at 9:32 am

    This is dictated by § 27.7.3.6.4 of the C++11 Standard, where the following two overloaded operators (among others) are specified:

    template<class charT, class traits>
    basic_ostream<charT,traits>& operator<<(
        basic_ostream<charT,traits>& out, 
        const charT* s
        );
    
    template<class charT, class traits>
    basic_ostream<charT,traits>& operator<<(
        basic_ostream<charT,traits>& out, 
        const char* s
        );
    

    The last overload deals explicitly with char-based C-strings. This means that even for instantiations of the basic_ostream<> class template with the argument wchar_t there will be one overload which will deal with narrow char strings.

    Moreover, per § 27.7.3.6.4/5:

    Padding is determined as described in 22.4.2.2.2. The n characters starting at s are widened using out.widen (27.5.5.3). The widened characters and any required padding are inserted into out. Calls width(0).


    On the other hand, the statement wcout << string("Hello"); does not compile because string does not have an implicit conversion to const char*, and because there is no overload of operator << that would insert a string built with one character type into an output stream with a different underlying character type.

    In Standard terms (see § 21.4.8.9), here is how the definition of the overloaded operator << looks like for std::string:

    template<class charT, class traits, class Allocator>
    basic_ostream<charT, traits>& operator<<(
        basic_ostream<charT, traits>& os,
        const basic_string<charT,traits,Allocator>& str
        );
    

    As you can see, the same template parameter charT is used to instantiate both basic_ostream and basic_string.

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

Sidebar

Related Questions

#include <iostream> #include <string.h> using namespace std; int main() { int e=0; int b=0;
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
#include iostream #include string using namespace std; #define AA(bb) \ string(::##bb); int main (int
Running this code: #include <string> #include <iostream> using namespace std; int main() { string
How does string expressions in C++ work? Consider: #include <iostream> using namespace std; int
//C++ Example #include <iostream> using namespace std; int doHello (std::string&); int main() { std::string
#include <iostream> #include <string.h> using namespace std; int main() { char firstname[] = Alfred;
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { locale system();
#include <iostream> using namespace std #include <string.h> int main(){ char token[] = some random

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.