69941

C++ nested constructor calls vs. function declaration

What is the difference between the code snippets labeled "version 1" and "version 2" in the following code section:

int main() { using namespace std; typedef istream_iterator<int> input; // version 1) //vector<int> v(input(cin), input()); // version 2) input endofinput; vector<int> v(input(cin), endofinput); }

As far as I understand "version 1" is treated as function declaration. But I don't understand why nor what the arguments of the resulting function v with return type vector<int> are.

Answer1:

why

Because the Standard says, more or less, that anything that can possibly be interpreted as a function declaration will be, in any context, no matter what.

what the arguments... are

You might not believe this, but it's true. input(cin) is treated as input cin; in this spot, parentheses are allowed and simply meaningless. However, input() is not treated as declaring a parameter of type input with no name; instead, it is a parameter of type input(*)(), i.e. a pointer to a function taking no arguments and returning an input. The (*) part is unnecessary in declaring the type, apparently. I guess for the same reason that the & is optional when you use a function name to initialize the function pointer...

Another way to get around this, taking advantage of the fact that we're declaring the values separately anyway to justify skipping the typedef:

istream_iterator<int> start(cin), end; vector<int> v(start, end);

Another way is to add parentheses in a way that isn't allowed for function declarations:

vector<int> v((input(cin)), input());

For more information, Google "c++ most vexing parse".

Answer2:

This is called most vexing parse :

This snippet :

input()

could be disambiguated either as

<ol> <li>a variable definition for variable class input, taking an anonymous instance of class input or</li> <li>a function declaration for a function which returns an object of type input and takes a single (unnamed) argument which is a function returning type input (and taking no input).</li> </ol>

Most programmers expect the first, but the C++ standard requires it to be interpreted as the second.

Answer3:

vector<int> v(input(cin), input());

Well, the arguments to this function declaration are these:

    <li>input(cin) - which is an object</li> <li>input (*)() - which is a pointer to function returning input and taking no argument.</li> </ul>

Recommend

  • python: geometric brownian motion simulation [closed]
  • Transforming multiindex to row-wise multi-dimensional NumPy array.
  • PHP: black image when cropping using php
  • In C,why is definition of a global variable in a separate statement raising warning,but is OK for a
  • Safe regexs to clean serialized DOM?
  • Why are `colMeans()` and `rowMeans()` functions faster than using the mean function with `lapply()`?
  • Sweave + RweaveHTML: cat output does not appear in the output
  • bit shift multiplication in c not using powers of 2 [duplicate]
  • How to crop a mp3 in ASP.NET + C#?
  • What is wrong in my MVC implementation?
  • C++ String tokenisation from 3D .obj files
  • Scala split a multi line string by lines that contain all hyphens
  • 'include' of functions in groovy scripts
  • Excel passing a range into a function
  • android-support-v7-appcompat has same attrs as actionbarsherlock library
  • What is the likely cause of a net::ERR_CONNECTION_ABORTED when uploading a file to Spring
  • Insert records if not exist SQL Server 2005
  • Does the MySQL IN clause execute the subquery multiple times?
  • Can XOR be expressed using SKI combinators?
  • How dotnet build chooses the output name
  • Unique Permutations - with exceptions
  • What Makes These Two Array Adds Different?
  • hide missing dates from x-axis ggplot2
  • NUnit 3.0 TestCase const custom object arguments
  • JPA flush vs commit
  • Elasticsearch script query involving root and nested values
  • Why use database factory in asp.net mvc?
  • Cannot upload to OneDrive using the new SDK
  • How do I configure context broker accept post requests from my remote sensor?
  • What's the purpose of QString?
  • How to use carriage return with multiple line?
  • How do I open a C file with a relative path?
  • Control modification in presentation layer
  • Illegal mix of collations for operation for date/time comparison
  • Javascript Callbacks with Object constructor
  • MySQL WHERE-condition in procedure ignored
  • Adding custom controls to a full screen movie
  • Calling of Constructors in a Java
  • Confusion with PayPal's monthly billing cycle
  • How to Embed XSL into XML