-4

What is the difference between the following ways of printing a string?

std::cout << "hello";

// and

std::cout << std::string("hello");

They both appear to do the same thing.

cigien
  • 57,834
  • 11
  • 73
  • 112
MaskOryle
  • 31
  • 4
  • There's no difference actually. Check the assembly code emitted by the compiler. – πάντα ῥεῖ Mar 06 '22 at 17:53
  • You are missing `#include ` and you should rather use `(std::string)"hello"` – Jeffrey Mar 06 '22 at 17:53
  • The second one has more letters than the first one. The second one uses parentheses, and the first one has no parentheses. – Eljay Mar 06 '22 at 17:53
  • the second one has 8 more characters. Also this code wont compile as thats an ilegal comment. Plus dont do `using namespace std` see https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – pm100 Mar 06 '22 at 17:55
  • One invokes `operator<<` overload taking `const char*`, the other an overload taking `std::string`. Both calls will produce the same output. – Igor Tandetnik Mar 06 '22 at 17:56
  • I thought humanity was able to figure out that this is a question with an example, and not the actual code... I was wrong. – MaskOryle Mar 06 '22 at 17:56
  • 2
    @Jeffrey `(std::string)"hello"` and `std::string("hello")` do exactly the same, only that the C-style cast form is often discouraged in C++. – user17732522 Mar 06 '22 at 17:56
  • @πάνταῥεῖ godbolt would disagree with you on that – pm100 Mar 06 '22 at 18:00
  • 1
    This appears to be a clear question, and I'm not sure what additional details are required. I've cleaned it up slightly, if that helps. There doesn't appear to be a duplicate target for this question (or at least, I couldn't find one), so I'm voting to reopen. – cigien Mar 06 '22 at 18:07
  • @MaskOryle was your question answered by any of the answers or are there details missing that you'd like us to add? – Ted Lyngmo Mar 09 '22 at 11:42

2 Answers2

1

std::cout << "hello"; uses the operator<< overload dealing with null terminated character arrays. "hello", which is a const char[6], decay s into a const char* when passed as an argument to the operator<< overload:

template< class CharT, class Traits >
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
                                         const char* s );

std::cout << std::string("hello"); uses the operator<< overload for dealing with strings. std::string is a typedef for std::basic_string<char, std::char_traits<char>, std::allocator<char>>.

template <class CharT, class Traits, class Allocator>
std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os,
               const std::basic_string<CharT, Traits, Allocator>& str);
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
1

Case 1

Here we consider the statement:

std::cout << "hello";

In the above statement "hello" is a string literal of type const char[6] which decays to a const char* due to type decay. This const char* is then passed to the non-member overload of operator<< given below:

template< class CharT, class Traits >
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
                                         const char* s );

Case 2

Here we consider the statement:

std::cout << std::string("hello");

In the above statement the expression std::string("hello") creates a temporary std::string object. Now since the std::string has overloaded operator<<(given below), that overloaded version of operator<< will be called and its body will be executed.

template <class CharT, class Traits, class Allocator>
std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os,
               const std::basic_string<CharT, Traits, Allocator>& str);
Jason
  • 36,170
  • 5
  • 26
  • 60