most-vexing-parse

How does this declaration invoke the Most Vexing Parse?

青春壹個敷衍的年華 提交于 2020-01-09 11:33:31
问题 Consider the following program: #include <fstream> struct A {}; int main(int argc, char** argv) { A a(std::fstream(argv[1])); } Clang in C++1y mode reckons that the MVP is invoked such that a is parsed as a function declaration: clang++ -std=c++1y -O3 -Wall -Wextra -pedantic-errors -pthread main.cpp && ./a.out main.cpp:6:8: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse] A a(std::fstream(argv[1])); ^~~~~~~~~~~~~~~~~~~~~~~ main.cpp:6:9: note: add a pair of

How does this declaration invoke the Most Vexing Parse?

匆匆过客 提交于 2020-01-09 11:32:25
问题 Consider the following program: #include <fstream> struct A {}; int main(int argc, char** argv) { A a(std::fstream(argv[1])); } Clang in C++1y mode reckons that the MVP is invoked such that a is parsed as a function declaration: clang++ -std=c++1y -O3 -Wall -Wextra -pedantic-errors -pthread main.cpp && ./a.out main.cpp:6:8: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse] A a(std::fstream(argv[1])); ^~~~~~~~~~~~~~~~~~~~~~~ main.cpp:6:9: note: add a pair of

Default constructor with empty brackets

时光总嘲笑我的痴心妄想 提交于 2020-01-06 23:42:31
问题 Is there any good reason that an empty set of round brackets (parentheses) isn't valid for calling the default constructor in C++? MyObject object; // ok - default ctor MyObject object(blah); // ok MyObject object(); // error I seem to type "()" automatically everytime. Is there a good reason this isn't allowed? 回答1: Most vexing parse This is related to what is known as "C++'s most vexing parse". Basically, anything that can be interpreted by the compiler as a function declaration will be

Overloaded operator << outputs bool value. why?

拟墨画扇 提交于 2020-01-06 19:36:35
问题 xml_attribute.h #pragma once #ifndef XML_ATTRIBUTET_H #define XML_ATTRIBUTET_H #include <string> #include <iostream> struct XML_AttributeT{ std::string tag; std::string value; //constructors explicit XML_AttributeT(std::string const& tag, std::string const& value); explicit XML_AttributeT(void); //overloaded extraction operator friend std::ostream& operator << (std::ostream &out, XML_AttributeT const& attribute); }; #endif xml_attribute.cpp #include "xml_attribute.h" //Constructors XML

constructor does not run

允我心安 提交于 2019-12-31 07:34:47
问题 I do not understand because when you create an object of the "Users" class not the message is printed containing the constructor. class users { public: users(); private: int i; }; users::users () { cout<<"hello world"; } int main () { users users1(); return 0; } 回答1: users users1(); doesn't declare an object of the users class, it declares a function that takes no arguments and returns an object of the users class. To declare an object, use: users users1; 回答2: class users { public: users();

How is this a most vexing parse?

夙愿已清 提交于 2019-12-30 20:52:53
问题 I was going through this article and there is a statement in item 3 saying // C++98 rectangle w( origin(), extents() ); // oops, vexing parse how is the above a most vexing parse. If I did something like this struct origin { }; struct Rectangle { Rectangle(const origin& s) { } }; The statement Rectangle s(origin()); works fine and does not resemble a vexing parse. Why did the author say that its a vexing parse. Is that a typo or am I missing something ? 回答1: Rectangle s(origin()); is a vexing

Construction of temporary in function call is interpreted as declaration

穿精又带淫゛_ 提交于 2019-12-29 08:33:09
问题 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<

Does specifying the use of void in the declaration of a function that takes no arguments address The Most Vexing Parse?

女生的网名这么多〃 提交于 2019-12-25 06:46:22
问题 Is the Most Vexing Parse rooted in the ambiguity about whether or not to use void as the parameter of a function declaration that takes no arguments? As an example, the following code compiles without error, and runs fine, on both the g++ (v7.2.1) and Xcode (Apple LLVM version 7.0.2 (clang-700.1.81)) compilers. #include <iostream> int asdf(void); int asdf(int a) { return a; } int main() { std::cout << asdf(6) << std::endl; //-> 6 return 0; } This goes all the way back to the ANSI-C standard

Why can this code elide a copy? [duplicate]

江枫思渺然 提交于 2019-12-23 15:58:54
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: constructor invocation mechanism Why is it an error to use an empty set of brackets to call a constructor with no arguments? Why can this code elide all copies of A? #include <iostream> class A { public: A() {} A(const A&) { std::cout << "Copy" << std::endl; } }; class B { public: B(const A& a_) : a(a_) {} private: A a; }; int main() { B b(A()); } This code apparently makes no copies of A , and outputs nothing

Why vexing parse in an if condition? [duplicate]

守給你的承諾、 提交于 2019-12-22 11:04:50
问题 This question already has answers here : Declaring class variable inside an if statement (2 answers) Closed 3 years ago . Consider the code: #include <iostream> struct Foo { Foo(int){} operator bool() const { return true; } }; int main() { if(Foo foo{42}) { std::cout << "ok\n"; } } It compiles fine under gcc5. However, if I replace the line if(Foo foo{42}) with if(Foo foo(42)) I get a compile-time error: error: expected primary-expression before 'foo' What's going on here? There is no vexing