most-vexing-parse

C++ spooky constructor [duplicate]

最后都变了- 提交于 2019-11-30 23:14:44
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why is it an error to use an empty set of brackets to call a constructor with no arguments? Lets have this code class Foo { Foo(int) { } }; Then we have there results: int main() { Foo f1 = Foo(5); // 1: OK, explicit call Foo f2(5); // 2: OK, implicit call Foo f3(); // 3: no error, "f3 is a non-class type Foo()", how so? Foo f4(f1); // 4: OK, implicit call to default copy constructor Foo f5; // 5: expected error

Difference between parsing of void() and int()

旧城冷巷雨未停 提交于 2019-11-30 17:48:26
Having read about the most vexing parse, I experimented a bit and found this program. There are two very similar lines. One of them yields warnings in both g++7 and clang++-3.9, another does not. int main() { void(); // no warning int(); // warning: statement has no effect } In the second line a default-constructed object of type int is created and immediately destroyed, thus unused. But what happens in the first line? If it was parsed the same way, it should be an error because it is illegal to create an object of type void . On the other hand, it does not look like a function declaration as

Difference between parsing of void() and int()

独自空忆成欢 提交于 2019-11-30 01:38:54
问题 Having read about the most vexing parse, I experimented a bit and found this program. There are two very similar lines. One of them yields warnings in both g++7 and clang++-3.9, another does not. int main() { void(); // no warning int(); // warning: statement has no effect } In the second line a default-constructed object of type int is created and immediately destroyed, thus unused. But what happens in the first line? If it was parsed the same way, it should be an error because it is illegal

Construction of temporary in function call is interpreted as declaration

你。 提交于 2019-11-29 13:36:19
Lately I ran into a problem which somehow (but only somehow) makes sense to me. It is based on interpreting the construction of a temporary as declaration of the single (!) constructor argument. Please have a look at the minimal example below. #include <iostream> class Foo0{ public: Foo0(int a){}; void doStuff() {std::cout<<"maap"<<std::endl;}; }; class Foo1{ public: Foo1(int a){}; void doStuff() {std::cout<<"maap"<<std::endl;}; }; class Foo2{ public: Foo2(int a){}; void doStuff() {std::cout<<"maap"<<std::endl;}; }; class Bar{ public: Bar(Foo0 foo0, Foo1 foo1, Foo2 foo2){}; }; int main () {

g++ rejects, clang++ accepts: foo(x)(“bar”)(“baz”);

僤鯓⒐⒋嵵緔 提交于 2019-11-29 13:07:42
Somebody had asked the other day why something compiles with clang, but not with gcc. I intuitively understood what was happening and was able to help the person, but it got me wondering -- according to the standard, which compiler was correct? Here is a boiled down version of the code: #include <iostream> #include <string> class foo { public: foo(const std::string& x): name(x) { } foo& operator()(const std::string& x) { std::cout << name << ": " << x << std::endl; return (*this); } std::string name; }; int main() { std::string x = "foo"; foo(x)("bar")("baz"); return 0; } This compiles fine

Vector constructor with two parameters is parsed as a function declaration

99封情书 提交于 2019-11-29 11:02:34
Consider this example: #include <iostream> #include <string> #include <vector> #include <iterator> int main() { std::string sen = "abc def ghi jkl"; std::istringstream iss(sen); std::vector<std::string> // declaration in question vec(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>()); std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(std::cout, "\n")); } The compiler throws an error at the call to std::copy request for member 'begin' in 'vec', which is of non-class type... I can get around the error like this: std::istream_iterator<std::string> it

What's the differences between Test t; and Test t();? if Test is a class [duplicate]

微笑、不失礼 提交于 2019-11-28 07:48:53
Possible Duplicate: Why is there no call to the constructor? I am using Visual studio 2012, Suppose Test is a class class Test { }; When I create a new instance of Test, what's the difference of the following two ways? way 1 Test t; way 2 Test t(); I got this question in the code below, originally, I defined an instance of A in way 2, I got only one error because B does not provide an default constructor, but when I define it in way 1, I got an extra error. class B { B(int i){} }; class A { A(){} B b; }; int main(void) { A a(); // define object a in way 2 getchar() ; return 0 ; } if I define a

g++ rejects, clang++ accepts: foo(x)(“bar”)(“baz”);

你离开我真会死。 提交于 2019-11-28 06:56:51
问题 Somebody had asked the other day why something compiles with clang, but not with gcc. I intuitively understood what was happening and was able to help the person, but it got me wondering -- according to the standard, which compiler was correct? Here is a boiled down version of the code: #include <iostream> #include <string> class foo { public: foo(const std::string& x): name(x) { } foo& operator()(const std::string& x) { std::cout << name << ": " << x << std::endl; return (*this); } std:

Vector constructor with two parameters is parsed as a function declaration

拟墨画扇 提交于 2019-11-28 04:43:45
问题 Consider this example: #include <iostream> #include <string> #include <vector> #include <iterator> int main() { std::string sen = "abc def ghi jkl"; std::istringstream iss(sen); std::vector<std::string> // declaration in question vec(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>()); std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(std::cout, "\n")); } The compiler throws an error at the call to std::copy request for member 'begin' in 'vec',

Difference between creating object with () or without

喜你入骨 提交于 2019-11-27 11:36:37
i just run into the problem error: request for member ‘show’ in ‘myWindow’, which is of non-class type ‘MainGUIWindow()’ when trying to compile a simple qt-application: #include <QApplication> #include "gui/MainGUIWindow.h" int main( int argc, char** argv ) { QApplication app( argc, argv ); MainGUIWindow myWindow(); myWindow.show(); return app.exec(); } I solved this by replacing MainGUIWindow myWindow(); by MainGUIWindow myWindow; but I don't understand the difference. My question: What is the difference? Regards, Dirk Armen Tsirunyan The other answers correctly state that the parentheses