hi, i am reading the book "The c++ standard library -- a tutorial and reference"{N.M.Josuttis}

in the beggining of the 6th chapter it says{in regard to container initialization}::

std::deque<int> c(
		(std::istream_iterator<int>(std::cin)),
                  (std::istream_iterator<int>())  
		  );

{the weird indentation is on purpose}

instead of::

std::deque<int> c(std::istream_iterator<int>(std::cin),
                  std::istream_iterator<int>());

because

In this case, c declares a function with a return type that is deque<int>. Its first
parameter is of type istream_iterator<int> with the name cin
{???}, and its second
unnamed parameter is of type "function taking no arguments returning
istream_iterator<int>." This construct is valid syntactically as either a declaration
or an expression. So, according to language rules, it is treated as a declaration. The extra
parentheses force the initializer not to match the syntax of a declaration.

so the question is how can the compiler think that this is function....since std::istream_iterator<int>(std::cin) is not a type.

>so the question is how can the compiler think that this is function
This is how it would be parsed:

deque<int> c ( istream_iterator<int> cin, istream_iterator<int> );

That looks exactly lot like a function declaration, doesn't it?

perhaps this would clarify (not different, but the types involved are simpler and therefore the example is easier to follow).

void foobar( int a, int b ) {}

int main()
{
  int a = 78 ;
  int foo( int(a) ) ; // is this the declaration (of a function foo)
                      // or the definition of an int foo?
  int(*pfn)(int) = &foo ; // it is a declaration!

  int bar( (int(a)) ) ; // can't be the declaration (of a function bar)
  int* pi = &bar ; // so, must be the definition of an int bar
  // reason (int(a)) is an expression because of parantheses; 

  // for example
  foobar( a, bar ) ; // ok
  // foobar( (a, bar) ) ; // error, too few arguments to function foobar
                       // because (a,bar) is *a single* expression
}

int foo( int(a) )
{
  return a+6 ;
}

there are several threads in comp.std.c++ which discuss why the language rules are framed this way, what would have been the alternatives etc. here is one of them http://groups.google.com/group/comp.std.c++/browse_thread/thread/57b52b0a1a40a9ad

commented: thanks for the example +1

thanks for your answers! vijayan your example example did clarify ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.