most-vexing-parse

C++ Most vexing parse when a number literal is the argument? [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-20 06:28:10
问题 This question already has an answer here : Why can in-class initializers only use = or {}? (1 answer) Closed last year . I was making a class that looked like this: struct InputHandler { std::vector<std::pair<int, int>> keyBindings( 256 ); }; It came up with an error, and I know this is because the compiler interprets this as a function instead of a constructor argument. But I was wondering is there anything ambiguous when I've passed a number literal in brackets such as in this case? I know

Pass anonymous function object to std::function?

岁酱吖の 提交于 2019-12-19 17:19:07
问题 Here is my question: I define a functor: class A { public: int operator()(int a, int b) const{ return a + b; } }; typedef function<int (int, int)> Fun; then I use a anonymous functor to create a std::function object, and I find something strange. Here is my code: Fun f(A()); f(3, 4); Unfortunately it is wrong. The error message is: error: invalid conversion from ‘int’ to ‘A (*)()’ [-fpermissive] error: too many arguments to function ‘Fun f(A (*)())’ However, when I change my code as follow: A

Pass anonymous function object to std::function?

一曲冷凌霜 提交于 2019-12-19 17:19:04
问题 Here is my question: I define a functor: class A { public: int operator()(int a, int b) const{ return a + b; } }; typedef function<int (int, int)> Fun; then I use a anonymous functor to create a std::function object, and I find something strange. Here is my code: Fun f(A()); f(3, 4); Unfortunately it is wrong. The error message is: error: invalid conversion from ‘int’ to ‘A (*)()’ [-fpermissive] error: too many arguments to function ‘Fun f(A (*)())’ However, when I change my code as follow: A

Default constructor c++

a 夏天 提交于 2019-12-19 04:22:22
问题 I am trying to understand how default constructor (provided by the compiler if you do not write one) versus your own default constructor works. So for example I wrote this simple class: class A { private: int x; public: A() { std::cout << "Default constructor called for A\n"; } A(int x) { std::cout << "Argument constructor called for A\n"; this->x = x; } }; int main (int argc, char const *argv[]) { A m; A p(0); A n(); return 0; } The output is : Default constructor called for A Argument

Default constructor c++

泄露秘密 提交于 2019-12-19 04:22:00
问题 I am trying to understand how default constructor (provided by the compiler if you do not write one) versus your own default constructor works. So for example I wrote this simple class: class A { private: int x; public: A() { std::cout << "Default constructor called for A\n"; } A(int x) { std::cout << "Argument constructor called for A\n"; this->x = x; } }; int main (int argc, char const *argv[]) { A m; A p(0); A n(); return 0; } The output is : Default constructor called for A Argument

C++ compile error constructing object with rvalue std::string

情到浓时终转凉″ 提交于 2019-12-18 18:54:29
问题 I'm faced with a compile error that I don't even know how to describe! It completely baffles me. The situation : Code tries to create an object on the stack with an rvalue std::string that is initialized with a char*. The code : #include <iostream> #include <string> class Foo { public: Foo(double d) : mD(d) { } Foo(const std::string& str) { try { mD = std::stod(str); } catch (...) { throw; } } Foo(const Foo& other) : mD(other.mD) { } virtual ~Foo() {} protected: double mD; }; class Bar {

Understanding 'most vexing parse' - why allow ambiguous syntax?

六眼飞鱼酱① 提交于 2019-12-18 07:03:17
问题 While trying to understand the "Most vexing parse" problem in C/C++, this question immediately springs to mind - why have a syntax that causes this problem to begin with? For example, class Timer { public: Timer(); }; class TimeKeeper { public: TimeKeeper(const Timer& t); int get_time() { return 1; } }; int main() { TimeKeeper time_keeper(Timer()); // the above is eq to this: TimeKeeper time_keeper(Timer (*)()); } So why not simply disallow TimeKeeper time_keeper(Timer()) to be a function

Is there any difference between `List x;` and `List x()`

我只是一个虾纸丫 提交于 2019-12-18 06:48:33
问题 The title comes from the famous site C++ FAQ by Marshall Cline. The author claims that there is a difference between the following two code examples. Suppose that List is the name of some class. Then function f() declares a local List object called x: void f() { List x; // Local object named x (of class List) ... } But function g() declares a function called x() that returns a List: void g() { List x(); // Function named x (that returns a List) ... } But is it really wrong to use the second

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

血红的双手。 提交于 2019-12-17 18:29:52
问题 This question already has answers here : Closed 7 years ago . 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

Why does this call the default constructor?

随声附和 提交于 2019-12-17 08:04:14
问题 struct X { X() { std::cout << "X()\n"; } X(int) { std::cout << "X(int)\n"; } }; const int answer = 42; int main() { X(answer); } I would have expected this to print either X(int) , because X(answer); could be interpreted as a cast from int to X , or nothing at all, because X(answer); could be interpreted as the declaration of a variable. However, it prints X(), and I have no idea why X(answer); would call the default constructor. BONUS POINTS: What would I have to change to get a temporary