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

vector<double> hour {vector<double>(24, -7777)}; <--> vector<double> hour(24, -7777);

43 views
Skip to first unread message

Christiano

unread,
Apr 24, 2017, 5:01:39 AM4/24/17
to
I'm reading a book which has the following code:

---Book-----------------------------------
struct Day {
vector<double> hour {vector<double>(24, -7777)};
}
------------------------------------------

And he explains:

--Book------------------------------------
Why didn't we write

struct Day {
vector<double> hour {24, -7777};
};

?

That would have been simpler, but unfortunately, we would have gotten a
vector of two elements (24 and -1). When we want to specify the number
of elements for a vector for which an integer can be converted to the
element type,
we unfortunately have to use the () initializer syntax.
------------------------------------------

Ok, I understand what he is saying. But is not the following code better?

struct Day {
vector<double> hour(24, -7777);
}

Is there any special reason why he chose to write in this way rather
than in the simplest form?



Book ISBN 978-0-321-99278-9

Christiano

unread,
Apr 24, 2017, 5:16:11 AM4/24/17
to
On 04/24/17 06:01, Christiano wrote:

>
> Is there any special reason why he chose to write in this way rather
> than in the simplest form?

Clarifying

"in this way" means:

struct Day {
vector<double> hour {vector<double>(24, -7777)};
}

"simplest form" means:

Alf P. Steinbach

unread,
Apr 24, 2017, 8:45:57 AM4/24/17
to
On 24-Apr-17 11:01 AM, Christiano wrote:
> I'm reading a book which has the following code:
>
> ---Book-----------------------------------
> struct Day {
> vector<double> hour {vector<double>(24, -7777)};
> }
> ------------------------------------------
>
> And he explains:
>
> --Book------------------------------------
> Why didn't we write
>
> struct Day {
> vector<double> hour {24, -7777};
> };
>
> ?
>
> That would have been simpler, but unfortunately, we would have gotten a
> vector of two elements (24 and -1). When we want to specify the number
> of elements for a vector for which an integer can be converted to the
> element type,
> we unfortunately have to use the () initializer syntax.
> ------------------------------------------
>
> Ok, I understand what he is saying. But is not the following code better?
>
> struct Day {
> vector<double> hour(24, -7777);
> }

It would be simpler if that syntax were supported.

Initializers for data members were added as an afterthought in C++11, to
avoid the redundancy of specifying the same initialization in each
constructor.

The standardization committee had to work within the existing awkward
syntax where round parenteses have just too many meanings, e.g. yielding
the so called “most vexing parse” of C++. They chose to support only
curly braces initializers, in two forms: direct initialization `T
m{...}` and copy initialization `T m = {...}`.


> Is there any special reason why he chose to write in this way rather
> than in the simplest form?

I imagine it's for pedagogical purposes, to illustrate the point above.

Otherwise, just write

struct Day
{
array<double, 24> hour{};
};

with the values initialized to 0.

IMHO that's roughly the simplest form, except that the purpose of this
struct is unclear.


Cheers & hth.,

- Alf

Alf P. Steinbach

unread,
Apr 24, 2017, 8:51:03 AM4/24/17
to
On 24-Apr-17 2:45 PM, Alf P. Steinbach wrote:
>
> The standardization committee had to work within the existing awkward
> syntax where round parenteses have just too many meanings, e.g. yielding
> the so called “most vexing parse” of C++. They chose to support only
> curly braces initializers, in two forms: direct initialization `T
> m{...}` and copy initialization `T m = {...}`.

Sorry, that was an awkward and misleading formulation.

You /can/ write

struct S
{
int x = 1;
};

and you can write

struct S
{
int x{ 1 };
};

but not -- there's no syntax for it --


struct S
{
int x( 1 );
};


Cheers!,

- Alf (now heading for coffee)

Christiano

unread,
Apr 24, 2017, 5:38:03 PM4/24/17
to
My mistake. I assumed it would work without taking the test before.

The code bellow doesn't work:
struct X
{
vector<int> a(20, -7777);
};

And the two codes bellow work:

struct X
{
vector<int> a = vector<int>(20, -7777);
};

struct X
{
vector<int> a {vector<int>(20, -7777)};
};

But using clang version 3.8.0 only works when using -std=c++11.

Thank you, Alf and Stefan.

Juha Nieminen

unread,
Apr 26, 2017, 2:18:30 AM4/26/17
to
Christiano <chris...@engineer.com> wrote:
> vector<double> hour {24, -7777};

> That would have been simpler, but unfortunately, we would have gotten a
> vector of two elements (24 and -1).

I can't even begin to imagine how -7777 is converted to -1 when cast to
a double.

Christiano

unread,
Apr 26, 2017, 8:07:44 PM4/26/17
to
You're right. I had not noticed that.
The book defines two constants:

const int not_a_reading = -7777;

And

const int not_a_month = -1;

and the book actually says:

"""""book"""""""""""""""""
[...]
Why didn't we write

struct Day {
vector<double> hour {24, not_a_reading};
};

That would have been simpler, but unfortunately, we would have gotten a
vector of two elements (24 and -1).
[...]
"""""""""""""""""""""""""

The correct would be

"""""correction"""""""""""""""""
[...]
Why didn't we write

struct Day {
vector<double> hour {24, not_a_reading};
};

That would have been simpler, but unfortunately, we would have gotten a
vector of two elements (24 and -7777).
[...]
"""""""""""""""""""""""""

The author probably confused between not_a_reading and not_a_month.

Verifying that you are right:

$ cat number.cpp
#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<double> v {24, -7777};

cout << "Len: " << v.size() << ", v[0]: " << v[0] << ", v[1]: "
<< v[1] << "\n";
}
$ CC number.cpp -std=c++11
$ ./a.out
Len: 2, v[0]: 24, v[1]: -7777
$

I will report this bug in the ppp-public group:
https://groups.google.com/forum/#!forum/ppp-public
Thank you.
0 new messages