Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Need to #include <string>?

50 views
Skip to first unread message
Message has been deleted
Message has been deleted

Victor Bazarov

unread,
Jun 5, 2015, 7:44:07 AM6/5/15
to
On 6/5/2015 7:00 AM, Stefan Ram wrote:
> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>> { ::std::cout <<( "a"s + "b"s )<< "\n"s;
>> ::std::cout <<( "a"s < "b"s )<< "\n"s; }
>
> And without the »+« and »<«, is »#include <string>«
> already required for »::std:cout << "\n"s«?

The 's' suffix is user-defined, isn't it? It's some kind of language
(library) extension or did I miss it in the Standard somewhere? If your
code that has the 's' suffix compiles OK without explicit inclusion of
<string>, why are you concerned with '+' needing it? Seems evident to
me that in order to use the 's' suffix you either rely on the indirect
inclusion of it, or do it explicitly...

V
--
I do not respond to top-posted replies, please don't ask

Louis Krupp

unread,
Jun 5, 2015, 8:03:17 AM6/5/15
to
I found the 's' suffix here:

http://en.cppreference.com/w/cpp/language/user_literal

<string> is required, it seems, but if it happens to be #included by
<iostream> or <ostream>, then the program compiles without explicit
inclusion. It's not a good idea, though; it would be too easy for
another programmer to move the 'cout' somewhere else, remove the
<ostream> and <stream> #includes, and then be surprised when the
remaining code doesn't compile.

Louis

Daniel

unread,
Jun 5, 2015, 8:12:45 AM6/5/15
to
On Friday, June 5, 2015 at 7:44:07 AM UTC-4, Victor Bazarov wrote:
>
> The 's' suffix is user-defined, isn't it? It's some kind of language
> (library) extension or did I miss it in the Standard somewhere?

It's in standard C++14, converts a character array literal to basic_string.

Daniel

Victor Bazarov

unread,
Jun 5, 2015, 8:57:31 AM6/5/15
to
OK, thanks. So, since 'basic_string' is a template declared in
<string>, it must be included to provide the conversion, so there is no
surprise that operator+ is also resolved, right?
Message has been deleted

Bo Persson

unread,
Jun 5, 2015, 11:40:48 AM6/5/15
to
On 2015-06-05 12:50, Stefan Ram wrote:
> The program
>
> #include <iostream>
> #include <ostream>
> using namespace ::std::literals;
> int main()
> { ::std::cout <<( "a"s + "b"s )<< "\n"s;
> ::std::cout <<( "a"s < "b"s )<< "\n"s; }
>
> prints
>
> ab
> 1
>
> here. But I am correct when I suspect that it is
> actually wrong, because it actually needs to
>
> #include <string>
>
> ?
>

Yes, you should include all the headers you need.

On the other hand, any standard C++ header is allowed to include any
other standard C++ header, so sometimes the header you need happens to
be included anyway.


Bo Persson

Message has been deleted

Victor Bazarov

unread,
Jun 5, 2015, 12:31:14 PM6/5/15
to
On 6/5/2015 11:59 AM, Stefan Ram wrote:
> Bo Persson <b...@gmb.dk> writes:
>> On 2015-06-05 12:50, Stefan Ram wrote:
>>> The program
>>> #include <iostream>
>>> #include <ostream>
>>> using namespace ::std::literals;
>>> int main()
>>> { ::std::cout <<( "a"s + "b"s )<< "\n"s;
>>> ::std::cout <<( "a"s < "b"s )<< "\n"s; }
>>> prints
>>> ab
>>> 1
>>> here. But I am correct when I suspect that it is
>>> actually wrong, because it actually needs to
>>> #include <string>
>>> ?
>> Yes, you should include all the headers you need.
>
> And /do/ I need the header »<string>« for
>
> ::std::cout << ""s;

You need it for

""s

AFAICT, regardless of how you use it.

>
> ? After all, AFAIK, I don't need »<cstring>« for
>
> ::std::cout << "";

No, this is part of <ostream>, included by <iostream>.

>
> . (And what about »""s+""s«?)
>

Since to use

""s

requires <string>, anything else is moot, isn't it?

Richard

unread,
Jun 6, 2015, 1:51:44 AM6/6/15
to
[Please do not mail me a copy of your followup]

Victor Bazarov <v.ba...@comcast.invalid> spake the secret code
<mks1vh$8jf$1...@dont-email.me> thusly:

>On 6/5/2015 7:00 AM, Stefan Ram wrote:
>> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>>> { ::std::cout <<( "a"s + "b"s )<< "\n"s;
>>> ::std::cout <<( "a"s < "b"s )<< "\n"s; }
>>
>> And without the »+« and »<«, is »#include <string>«
>> already required for »::std:cout << "\n"s«?
>
>The 's' suffix is user-defined, isn't it? It's some kind of language
>(library) extension or did I miss it in the Standard somewhere?

It's new to C++14.

<http://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s>
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Richard Damon

unread,
Jun 6, 2015, 10:05:51 AM6/6/15
to
On 6/5/15 8:03 AM, Louis Krupp wrote:
>
> <string> is required, it seems, but if it happens to be #included by
> <iostream> or <ostream>, then the program compiles without explicit
> inclusion. It's not a good idea, though; it would be too easy for
> another programmer to move the 'cout' somewhere else, remove the
> <ostream> and <stream> #includes, and then be surprised when the
> remaining code doesn't compile.
>
> Louis
>

In C, the language standard prohibits one system header from "randomly"
injecting names from other headers into the namespace, effectively
prohibiting one system header from including another defined system
header (they need to include "private" headers using symbols reserved
for the implementation for inter-header dependencies.

In C++, this doesn't work, so headers are allowed to include other
headers. The fundamental difference is that in C, type names don't
really matter. Code doesn't care if a type name is an actual struct or a
typedef for such a struct. In C++, it matters.

Yes, Good Practices say you should explicitly include what you use, and
not depend on indirect dependencies, but the language standard doesn't
provide any way it enforce such a rule.
0 new messages