-
Notifications
You must be signed in to change notification settings - Fork 5
/
list9.cc
57 lines (48 loc) · 2.24 KB
/
list9.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// -*- mode: c++; coding: utf-8 -*-
// ra-ra/test - Demo new warning in 9.1
// (c) Daniel Llorens - 2019
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
/*
https://gcc.gnu.org/onlinedocs/gcc-9.1.0/gcc/C_002b_002b-Dialect-Options.html#index-Winit-list-lifetime
When a list constructor stores the begin pointer from the initializer_list
argument, this doesn’t extend the lifetime of the array, so if a class variable
is constructed from a temporary initializer_list, the pointer is left dangling
by the end of the variable declaration statement.
<lloda> I don't understand this warning in 9.1 [13:06]
<lloda> https://wandbox.org/permlink/RTIL3P6nRanIx4Sd
<lloda> I've read
<lloda>
https://gcc.gnu.org/onlinedocs/gcc-9.1.0/gcc/C_002b_002b-Dialect-Options.html#index-Winit-list-lifetime
<lloda> the last item
<lloda> but doesn't it make a difference that I use p(a.begin()) and not
p(x.begin()) ?
<lloda> either gives the same warning
<redi> no, it makes no difference [13:07]
<redi> a and x are both just pointers to the same underlying array
<redi> the array is not owned by the initializer_list
<redi> do not store an initializer_list like that. ever. it's not a container.
<lloda> fair :O [13:08]
<lloda> then the { p = a.begin(); } should warn as well?
<redi> it's for INITIALIZING, not storing data
<redi> yes, ideally that would warn too
<redi> your comment says "works" but it's wrong. It doesn't work any
differently, it just doesn't warn. [13:09]
*/
// /opt/gcc-9.1/bin/g++ -o list9 -std=c++17 -Wall -Werror list9.cc
#include <utility>
struct Bar
{
std::initializer_list<int> a;
decltype(a.begin()) p;
// Bar(std::initializer_list<int> x): a(x), p(a.begin()) {} // warns
Bar(std::initializer_list<int> x): a(x) { p = a.begin(); } // doesn't (but same thing)
};
template <class T>
struct Foo
{
Foo(std::initializer_list<int> s): Foo(Bar(s)) {}
};
int main() {}