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

Re: Quick fun question

18 views
Skip to first unread message
Message has been deleted

Victor Bazarov

unread,
Jun 12, 2015, 11:04:12 PM6/12/15
to
On 6/12/2015 10:35 PM, Stefan Ram wrote:
> Quick! What does this print, if anything?
>
> #include <iostream>
> #include <ostream>
> #include <string>
>
> using namespace ::std::literals;
>
> int main() { ::std::cout << "s"s"s"s"s"s << '\n'; }

Looks like a syntax error. "s"s is interpreted as a std::string object
initialized with one letter 's', and what's the next token? Another
literal expression, which also yields an object. In other words,
between the << you wrote

std::string{"s",1} std::string{"s",1} std::string{"s",1}

and no operator between them. That's a syntax error, I think.

>
> Additional question for even more fun:
>
> What are the data types of the five expressions between the
> »<<« in the following program? Or is the program ill formed?
>
> #include <iostream>
> #include <ostream>
> #include <string>
>
> using namespace ::std::literals;
>
> int main()
> { ::std::cout << "s"s"s"s"s" << '\n';
> ::std::cout << "s""s"s"s"s << '\n';
> ::std::cout << "s""s"s"s" << '\n';
> ::std::cout << "s""s"s"s" << '\n';
> ::std::cout << "s""s""s" << '\n'; }
>
> Solution: See 2.13.5p13.
>

Solution for what? The "s"s is not a string literal, it's a
user-defined literal, to which the paragraph you named does not relate,
IMHO.

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

Victor Bazarov

unread,
Jun 12, 2015, 11:07:50 PM6/12/15
to
OK, I take it back. User-defined literals are treated as string
literals. Which document are you looking at, exactly? I am looking at
n4431, which is a draft.
Message has been deleted
Message has been deleted

Richard

unread,
Jun 12, 2015, 11:38:21 PM6/12/15
to
[Please do not mail me a copy of your followup]

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

>Solution for what? The "s"s is not a string literal, it's a
>user-defined literal, to which the paragraph you named does not relate,
>IMHO.

User-defined string literals participate in string literal
concatenation as long as only a single user-defined suffix is used on
all the string literals (or no suffix is used on some of the literals).

See <http://en.cppreference.com/w/cpp/language/user_literal> just
above the heading "Literal Operators".

Since C++14, <chrono> defines:
constexpr std::chrono::seconds operator""s(unsigned long long s)

and <string> defines:
constexpr std::string operator""s(const char *str, std::size_t len);
constexpr std::u16string operator""s(const char16_t *str, std::size_t len);
constexpr std::u32string operator""s(const char32_t *str, std::size_t len);
constexpr std::wstring operator""s(const wchar_t *str, std::size_t len);

What's kinda cool is that normal operator overloading makes it clear
that 54s is std::chrono::seconds and "54"s is a std::string, even when
both headers are included in the same source file.
--
"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>
0 new messages