prototype declaration not variable definition

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Harrison

    #1

    prototype declaration not variable definition

    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::istringstr eam buf("abc");
    std::vector<cha r> vec(std::istrea m_iterator<char >(buf),
    std::istream_it erator<char>()) ;

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

    std::istream_it erator<char>(bu f)

    is a parameter declaration with std::istream_it erator<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_it erator<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_it erator<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


  • Victor Bazarov

    #2
    Re: prototype declaration not variable definition

    "John Harrison" <john_andronicu s@hotmail.com> wrote...[color=blue]
    > I've seen cases before where it was intended to write a variable[/color]
    declaration[color=blue]
    > 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[/color]
    I've[color=blue]
    > understood it correctly.
    >
    > The code I wrote was.
    >
    > std::istringstr eam buf("abc");
    > std::vector<cha r> vec(std::istrea m_iterator<char >(buf),
    > std::istream_it erator<char>()) ;
    >
    > To my surprise vec was interpreted as a function prototype. After a bit of
    > thought here's how I see it
    >
    > std::istream_it erator<char>(bu f)
    >
    > is a parameter declaration with std::istream_it erator<char> as the type[/color]
    and[color=blue]
    > 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_it erator<char>()
    >
    > was being interpreted as a parameter declaration. Then it occured to me[/color]
    that[color=blue]
    > in function declarations you are allowed to omit the dummy parameter name,
    > so that std::istream_it erator<char>() could be interpreted as parameter
    > declaration with superfluous parens surrounding the missing dummy[/color]
    parameter[color=blue]
    > name!
    >
    > Is this a correct interpretation? Or am I barking?[/color]

    You're absolutely correct.

    Victor


    Comment

    • Buster Copley

      #3
      Re: prototype declaration not variable definition

      John Harrison wrote:[color=blue]
      > 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::istringstr eam buf("abc");
      > std::vector<cha r> vec(std::istrea m_iterator<char >(buf),
      > std::istream_it erator<char>()) ;
      >
      > To my surprise vec was interpreted as a function prototype. After a bit of
      > thought here's how I see it
      >
      > std::istream_it erator<char>(bu f)
      >
      > is a parameter declaration with std::istream_it erator<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_it erator<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_it erator<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[/color]

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

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

      std::istream_it erator <char> b;
      std::istream_it erator <char> f ();

      int main ()
      {
      std::istringstr eam buf ("abc");
      std::vector <char> vec (std::istream_i terator <char> (buf),
      std::istream_it erator <char> ());

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

      Regards,
      Buster.


      Comment

      • John Harrison

        #4
        Re: prototype declaration not variable definition

        > >[color=blue][color=green]
        > > Is this a correct interpretation? Or am I barking?
        > > john[/color]
        >
        > Close. The second parameter of vec is a pointer to a function taking no
        > arguments and returning std::istream_it erator <char>.
        >
        > #include <sstream>
        > #include <vector>
        > #include <iterator>
        >
        > std::istream_it erator <char> b;
        > std::istream_it erator <char> f ();
        >
        > int main ()
        > {
        > std::istringstr eam buf ("abc");
        > std::vector <char> vec (std::istream_i terator <char> (buf),
        > std::istream_it erator <char> ());
        >
        > sizeof vec (b, f); // use the declarations; no error
        > }
        >
        > Regards,
        > Buster.
        >[/color]

        That would be

        std::vector <char> vec (std::istream_i terator <char> (buf),
        std::istream_it erator <char> (*)());

        wouldn't it?

        john


        Comment

        • John Harrison

          #5
          Re: prototype declaration not variable definition


          "Buster Copley" <buster@none.co m> wrote in message
          news:bgso48$elf $1@newsg2.svr.p ol.co.uk...[color=blue][color=green][color=darkred]
          > >>Close. The second parameter of vec is a pointer to a function taking no
          > >>arguments and returning std::istream_it erator <char>.[/color][/color]
          >[color=green]
          > > That would be
          > >
          > > std::vector <char> vec (std::istream_i terator <char> (buf),
          > > std::istream_it erator <char> (*)());
          > >
          > > wouldn't it?
          > >
          > > john[/color]
          >
          > Yes, that would work too.
          > Buster
          >[/color]

          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


          Comment

          Working...