From the book "C ++ Programming Language", 4th edition, Section "17.3.1 Initialization without Constructors", Page 489
The marked line in the example from the book does not compile with this error -
$ g++ -std=c++11 ch17_pg489.cpp
ch17_pg489.cpp: In function 'int main()':
ch17_pg489.cpp:32:34: error: could not convert 's9' from 'Work' to 'std::string {aka std::basic_string<char>}'
Work currently_playing { s9 }; // copy initialization
I have cygwin
$ g++ --version
g++.exe (tdm64-2) 4.8.1
To quote the text from the above section,
we can initialize objects of a class for which we have not defined a constructor using
β’ memberwise initialization,
β’ copy initialization, or
β’ default initialization (without an initializer or with an empty initializer list).
#include <iostream>
struct Work {
std::string author;
std::string name;
int year;
};
int main() {
Work s9 { "Beethoven",
"Symphony No. 9 in D minor, Op. 125; Choral",
1824
};
Work currently_playing { s9 };
Work none {};
return 0;
}
According to my understanding, any copy initialization will be provided by the default copy constructor generated by the compiler. Or it will be just a wise copy of the member (assigning one structure to another, as in C). Therefore, the program should have been compiled here.
Or is it a quirk compiler ??
Any explanation?