Questions tagged [most-vexing-parse]

For questions about the "most vexing parse", a particular syntactic ambiguity in C++ programs that leads to a counterintuitive interpretation of certain declarations. (The name was coined by Scott Meyers in "Effective STL".) It is often accompanied by poor diagnostics, confusing many programmers who encounter it.

The most vexing parse refers to a particular C++ syntactic ambiguity where a declaration statement that can be interpreted either as an object declaration with an initializer that is a temporary object or as a function declaration is resolved, by rule, to a function declaration.

As an example, the line

MyObject obj(4);

declares an object of type MyObject named obj passing in 4 as a parameter. However, the line

MyObject obj(OtherType());

does not declare obj as an object constructed from a value initialized OtherType temporary, but instead declares a function named obj that takes a pointer to a function taking no arguments and returning an OtherType. (Function parameters declared as having function type are automatically adjusted to the corresponding pointer to function type in the same way that function parameters declared with array types are adjusted to the corresponding pointer type.)

To change the declaration into an object type the following alternatives can be used.

MyObject obj1( (OtherType()) ); // extra parentheses

MyObject obj1 = OtherType(); // Requires an implicit conversion between types
                             // and requires MyObject to be copyable (or movable
                             // in C++11)

MyObject obj1 {OtherType()}; // C++11 syntax
180 questions
1
vote
3 answers

Finding typeid of a template parameter

The print statement in the constructor's definition doesn't get printed, isn't the constructor calling correct in main? I know I am missing some point here, please point out. #include #include template class…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
1
vote
1 answer

iostream not printing to terminal in second source class (c++)

When I try to do a cout in a constructor it will not print out. I know cout works on my terminal because I can call it from the main(), but not from my CharacterStats.cpp class with a CharacterStats.hpp header. There is no terminal output like there…
hevansa98
  • 87
  • 1
  • 1
  • 8
1
vote
1 answer

Disable construction of non fully specialized template class

I have this kind of structure for a template class. My aim is to forbid creation of those class which not provide a full specialization: class AbstractContainer { public: virtual ~AbstractContainer() = default; // declare here interface…
Moia
  • 2,216
  • 1
  • 12
  • 34
1
vote
1 answer

error: request for member '..' in '..' , which is of non-class type

I use an STL priority_queue and give a custom comparator class whose constructor takes in the pointer to the vector that stores the priorities, thus - #include #include // std::priority_queue #include //…
1
vote
2 answers

Default constructor curiosity

While MyClass m(); is a classic gotcha, as it does not declare a variable m, but a function taking zero arguments and returning MyClass. However, I found that in Visual Studio the following statement produces the same problem (it seems it somehow…
DrPepperJo
  • 632
  • 1
  • 5
  • 19
1
vote
0 answers

c++ Compilation error 'parameter' declared as function returning a function

#include int main() { using namespace std; mt19937_64 gen1(random_device()());//fail? mt19937_64 gen2((random_device()()));//success mt19937_64 gen3{ random_device()() };//success mt19937_64…
iouvxz
  • 89
  • 9
  • 27
1
vote
1 answer

Variables does not have class type, even though it is defined

I am trying to write a class which defines a std::map. The comparator of the map must be a function pointer. The function pointer can be passed to the class as an argument in class's constructor. Below is the code I wrote: #include…
VinK
  • 25
  • 1
  • 7
1
vote
2 answers

cannot iterate on a vector

So, I want to read values from a .txt file, values are pretty simple four float values on the same line. I wrote this: int read_calibration () { std::ifstream input("floor.txt"); if (!input.good()) { cout << "floor info file does not…
Samer
  • 1,923
  • 3
  • 34
  • 54
1
vote
1 answer

Issue trying to use a template object inside a template class

Consider the following class definitions ... Node template class Node { private : T* data; Node* next; public : Node(T* data); void setData(T* data); T*…
Nimzaj
  • 25
  • 4
1
vote
1 answer

Passing a function object to a constructor

What I am trying to achieve is to make a functor that can take different functors as arguments. Edit: the reason for my problem, the "most vexing parse", and the solution are well-described: see this question and answer, the whole most-vexing-parse…
user1812457
1
vote
1 answer

How to properly bind rvalues to the constructor?

here is my code: #include class dummy{ public: //constructor + destructor dummy(){ std::cout<<"dummy constructed"<
Carlos Miguel Colanta
  • 2,685
  • 3
  • 31
  • 49
1
vote
4 answers

Convert time into minutes and seconds

I made a code to convert time into minutes and seconds using constructor. In output I'm getting time in seconds but its not displaying time in minutes and can't figured out my mistake so anyone can help me out... #include using namespace…
1
vote
1 answer

c++11 using function template parametrs in class defined in function

I have two questions about the code below: why version / * 1 * / compiles under g++ and version / * 2 * / not? why this code does not compile in clang? I know How to fix it. but I want to understand why it does not work. #include…
1
vote
1 answer

Unable to access private and public members from friend class

I have a class called locationdata which has a friend class called PointTwoD #include #include using namespace std; class locationdata { public: locationdata(); //default constructor …
Computernerd
  • 7,378
  • 18
  • 66
  • 95
1
vote
2 answers

what is the meaning of this syntax

The following code compiles. But if I write the code to call the method test using jar it is giving me a compilation error. What is really happening here. #include using namespace std; class A { public: void test() { cout <<…
user1198065
  • 210
  • 3
  • 10