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

Re: Is this an implicit use of the template?

74 views
Skip to first unread message

Alf P. Steinbach

unread,
Oct 31, 2015, 8:10:46 AM10/31/15
to
On 10/31/2015 11:59 AM, Stefan Ram wrote:
> The following program looks innocuous.
>
> #include <iostream>
> #include <ostream>
>
> int main() { constexpr int num{ 11 }; ::std::cout << num << '\n'; }
>
> Does the above constitute an »implicit use of the template
> »std::initializer_list«? I am thinking of 8.5.4p2:
>
> »The template std::initializer_list is not predefined; if the
> header <initializer_list> is not included prior to a use of
> std::initializer_list --- even an implicit use in which the
> type is not named (7.1.6.4) --- the program is ill-formed.«
>
> Or would only the following be such an implicit use?
>
> #include <iostream>
> #include <ostream>
>
> int main() { constexpr auto num{ 11 }; ::std::cout << num << '\n'; }
>

My gut feeling: None of these involve initializer_list, even though
they syntactically involve initializer-list (oh, the difference of a
hyphen: reminds me of Latin 1 versus Latin-1, not sure what that was).

They look like formally direct initializations.

And as far as I can recall the syntax

int x = {42};

(copy initialization, but with the braces so that it looks like an
initialization_list) has always been valid, from original C and onward.

So, my gut feeling is that you need something like

auto x = {42};

and then you'd be in initializer_list-land.


Cheers & hth.,

- Alf

Victor Bazarov

unread,
Oct 31, 2015, 8:44:57 AM10/31/15
to
On 10/31/2015 6:59 AM, Stefan Ram wrote:
> The following program looks innocuous.
>
> #include <iostream>
> #include <ostream>
>
> int main() { constexpr int num{ 11 }; ::std::cout << num << '\n'; }
>
> Does the above constitute an »implicit use of the template
> »std::initializer_list«? I am thinking of 8.5.4p2:
>
> »The template std::initializer_list is not predefined; if the
> header <initializer_list> is not included prior to a use of
> std::initializer_list --- even an implicit use in which the
> type is not named (7.1.6.4) --- the program is ill-formed.«
>
> Or would only the following be such an implicit use?
>
> #include <iostream>
> #include <ostream>
>
> int main() { constexpr auto num{ 11 }; ::std::cout << num << '\n'; }

How do you know that the header <initializer_list> is NOT included by
some directive in <iostream>, <ostream> or any headers they include? If
it's included, it's not "implicit use", is it?

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

Juha Nieminen

unread,
Nov 3, 2015, 4:11:58 AM11/3/15
to
Stefan Ram <r...@zedat.fu-berlin.de> wrote:
> #include <iostream>
> #include <ostream>

Isn't that second line superfluous?

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Alf P. Steinbach

unread,
Nov 3, 2015, 5:55:05 AM11/3/15
to
On 11/3/2015 10:11 AM, Juha Nieminen wrote:
> Stefan Ram <r...@zedat.fu-berlin.de> wrote:
>> #include <iostream>
>> #include <ostream>
>
> Isn't that second line superfluous?

In practice, always. But in the formal it was needed in C++98 and C++03.
All the examples in the standard were wrong. Which was a bit
embarrassing. Happily James Kanze proposed a fix, and now it's OK.

red floyd

unread,
Nov 3, 2015, 12:24:04 PM11/3/15
to
On 11/3/2015 1:11 AM, Juha Nieminen wrote:
> Stefan Ram <r...@zedat.fu-berlin.de> wrote:
>> #include <iostream>
>> #include <ostream>
>
> Isn't that second line superfluous?

Like Alf said, it was technically required in C++03. I don't know of
any implementation where it was needed, but technically, it was
required.

Juha Nieminen

unread,
Nov 4, 2015, 4:10:49 AM11/4/15
to
You need both to use std::cout? I can't even figure out how you could
impose that requirement, even if you deliberately tried to do so.
Except perhaps have std::cout declared in one, and its operators
in the other.

Martin Shobe

unread,
Nov 4, 2015, 9:15:37 AM11/4/15
to
On 11/3/2015 3:11 AM, Juha Nieminen wrote:
> Stefan Ram <r...@zedat.fu-berlin.de> wrote:
>> #include <iostream>
>> #include <ostream>
>
> Isn't that second line superfluous?

As far as I can tell, yes. (At least in n3797).

From 17.6.5.2p1

A C++ header may include other C++ headers. A C++ header shall
provide the declarations and definitions that appear in its synopsis. A
C++ header shown in its synopsis as including other C++ headers shall
provide the declarations and definitions that appear in the synopsis of
those other headers.

The synopsis for iostream includes ostream. (See 27.4.1).

Martin Shobe

Victor Bazarov

unread,
Nov 6, 2015, 8:03:30 PM11/6/15
to
On 11/4/2015 4:10 AM, Juha Nieminen wrote:
> red floyd <no....@its.invalid> wrote:
>> On 11/3/2015 1:11 AM, Juha Nieminen wrote:
>>> Stefan Ram <r...@zedat.fu-berlin.de> wrote:
>>>> #include <iostream>
>>>> #include <ostream>
>>>
>>> Isn't that second line superfluous?
>>
>> Like Alf said, it was technically required in C++03. I don't know of
>> any implementation where it was needed, but technically, it was
>> required.
>
> You need both to use std::cout? I can't even figure out how you could
> impose that requirement, even if you deliberately tried to do so.
> Except perhaps have std::cout declared in one, and its operators
> in the other.

At least that's how the Standard used to define those. <iostream> had
the declarations of the objects, and the classes were in the other
header. Do you think Alf and red were making it up?
0 new messages