prototype declaration not variable definition

Discussion in 'C++' started by John Harrison, Aug 7, 2003.

  1. I've seen cases before where it was intended to write a variable declaration
    but a function prototype was written instead (e.g. X x();) but I've come
    accross a new version of this (new to me at least) and I want to check I've
    understood it correctly.

    The code I wrote was.

    std::istringstream buf("abc");
    std::vector<char> vec(std::istream_iterator<char>(buf),
    std::istream_iterator<char>());

    To my surprise vec was interpreted as a function prototype. After a bit of
    thought here's how I see it

    std::istream_iterator<char>(buf)

    is a parameter declaration with std::istream_iterator<char> as the type and
    buf as the dummy parameter name, and also a superfluous pair of parens. So
    far so good, the bit that I couldn't work out was how

    std::istream_iterator<char>()

    was being interpreted as a parameter declaration. Then it occured to me that
    in function declarations you are allowed to omit the dummy parameter name,
    so that std::istream_iterator<char>() could be interpreted as parameter
    declaration with superfluous parens surrounding the missing dummy parameter
    name!

    Is this a correct interpretation? Or am I barking?

    john
     
    John Harrison, Aug 7, 2003
    #1
    1. Advertising

  2. "John Harrison" <> wrote...
    > I've seen cases before where it was intended to write a variable

    declaration
    > but a function prototype was written instead (e.g. X x();) but I've come
    > accross a new version of this (new to me at least) and I want to check

    I've
    > understood it correctly.
    >
    > The code I wrote was.
    >
    > std::istringstream buf("abc");
    > std::vector<char> vec(std::istream_iterator<char>(buf),
    > std::istream_iterator<char>());
    >
    > To my surprise vec was interpreted as a function prototype. After a bit of
    > thought here's how I see it
    >
    > std::istream_iterator<char>(buf)
    >
    > is a parameter declaration with std::istream_iterator<char> as the type

    and
    > buf as the dummy parameter name, and also a superfluous pair of parens. So
    > far so good, the bit that I couldn't work out was how
    >
    > std::istream_iterator<char>()
    >
    > was being interpreted as a parameter declaration. Then it occured to me

    that
    > in function declarations you are allowed to omit the dummy parameter name,
    > so that std::istream_iterator<char>() could be interpreted as parameter
    > declaration with superfluous parens surrounding the missing dummy

    parameter
    > name!
    >
    > Is this a correct interpretation? Or am I barking?


    You're absolutely correct.

    Victor
     
    Victor Bazarov, Aug 7, 2003
    #2
    1. Advertising

  3. John Harrison wrote:
    > I've seen cases before where it was intended to write a variable declaration
    > but a function prototype was written instead (e.g. X x();) but I've come
    > accross a new version of this (new to me at least) and I want to check I've
    > understood it correctly.
    >
    > The code I wrote was.
    >
    > std::istringstream buf("abc");
    > std::vector<char> vec(std::istream_iterator<char>(buf),
    > std::istream_iterator<char>());
    >
    > To my surprise vec was interpreted as a function prototype. After a bit of
    > thought here's how I see it
    >
    > std::istream_iterator<char>(buf)
    >
    > is a parameter declaration with std::istream_iterator<char> as the type and
    > buf as the dummy parameter name, and also a superfluous pair of parens. So
    > far so good, the bit that I couldn't work out was how
    >
    > std::istream_iterator<char>()
    >
    > was being interpreted as a parameter declaration. Then it occured to me that
    > in function declarations you are allowed to omit the dummy parameter name,
    > so that std::istream_iterator<char>() could be interpreted as parameter
    > declaration with superfluous parens surrounding the missing dummy parameter
    > name!
    >
    > Is this a correct interpretation? Or am I barking?
    > john


    Close. The second parameter of vec is a pointer to a function taking no
    arguments and returning std::istream_iterator <char>.

    #include <sstream>
    #include <vector>
    #include <iterator>

    std::istream_iterator <char> b;
    std::istream_iterator <char> f ();

    int main ()
    {
    std::istringstream buf ("abc");
    std::vector <char> vec (std::istream_iterator <char> (buf),
    std::istream_iterator <char> ());

    sizeof vec (b, f); // use the declarations; no error
    }

    Regards,
    Buster.
     
    Buster Copley, Aug 7, 2003
    #3
  4. > >
    > > Is this a correct interpretation? Or am I barking?
    > > john

    >
    > Close. The second parameter of vec is a pointer to a function taking no
    > arguments and returning std::istream_iterator <char>.
    >
    > #include <sstream>
    > #include <vector>
    > #include <iterator>
    >
    > std::istream_iterator <char> b;
    > std::istream_iterator <char> f ();
    >
    > int main ()
    > {
    > std::istringstream buf ("abc");
    > std::vector <char> vec (std::istream_iterator <char> (buf),
    > std::istream_iterator <char> ());
    >
    > sizeof vec (b, f); // use the declarations; no error
    > }
    >
    > Regards,
    > Buster.
    >


    That would be

    std::vector <char> vec (std::istream_iterator <char> (buf),
    std::istream_iterator <char> (*)());

    wouldn't it?

    john
     
    John Harrison, Aug 7, 2003
    #4
  5. "Buster Copley" <> wrote in message
    news:bgso48$elf$...
    > >>Close. The second parameter of vec is a pointer to a function taking no
    > >>arguments and returning std::istream_iterator <char>.

    >
    > > That would be
    > >
    > > std::vector <char> vec (std::istream_iterator <char> (buf),
    > > std::istream_iterator <char> (*)());
    > >
    > > wouldn't it?
    > >
    > > john

    >
    > Yes, that would work too.
    > Buster
    >


    But my point was I didn't realise

    void f(int ());

    was a synonym for

    void f(int (*)());

    How long has that been part of the language?

    And how is that interpretation of void f(int()) preferred over mine. Isn't
    that another ambiguity?

    john
     
    John Harrison, Aug 7, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Ward
    Replies:
    6
    Views:
    396
    White Wolf
    Sep 11, 2003
  2. Kristian Virkus
    Replies:
    7
    Views:
    409
    Thad Smith
    Feb 8, 2007
  3. vaib

    variable declaration and definition!

    vaib, Oct 5, 2007, in forum: C Programming
    Replies:
    17
    Views:
    529
  4. Pierre Yves
    Replies:
    2
    Views:
    489
    Pierre Yves
    Jan 10, 2008
  5. manish sahu

    Variable declaration Vs Definition

    manish sahu, Sep 26, 2009, in forum: C Programming
    Replies:
    2
    Views:
    1,768
    David Thompson
    Oct 5, 2009
Loading...

Share This Page