Topic: Limits of declaration syntax


Author: giecrilj@stegny.2a.pl ("Kristof Zelechovski")
Date: Sun, 10 Dec 2006 19:18:13 GMT
Raw View
Uzytkownik "Greg Herlihy" <greghe@pacbell.net> napisal w wiadomosci
news:1165409202.575999.37530@n67g2000cwd.googlegroups.com...
>
> John Nagle wrote:
>> This is valid, but requires typedefs.
>> Can this return type be written in a single statement?
>> Or does that exceed the capabilities of the declaration syntax?
>>
>> typedef char array3[3];
>> typedef array3& array3ref;
>>
>> array3ref foo();
>
> I'm not sure whether a valid declaration ever requires a typedef -

operator char (&(void))[02], anyone?
Chris


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: John Nagle <nagle@animats.com>
Date: Wed, 6 Dec 2006 00:38:30 CST
Raw View
This is valid, but requires typedefs.
Can this return type be written in a single statement?
Or does that exceed the capabilities of the declaration syntax?

 typedef char array3[3];
 typedef array3& array3ref;

 array3ref foo();


    John Nagle
    Animats

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Greg Herlihy" <greghe@pacbell.net>
Date: Wed, 6 Dec 2006 09:38:28 CST
Raw View
John Nagle wrote:
> This is valid, but requires typedefs.
> Can this return type be written in a single statement?
> Or does that exceed the capabilities of the declaration syntax?
>
>  typedef char array3[3];
>  typedef array3& array3ref;
>
>  array3ref foo();

I'm not sure whether a valid declaration ever requires a typedef -
though they can certainly help. I believe in this case the equivalent
declaration without resorting to a typedef would be:

    char ( &foo() )[3];

Greg

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Andrew Koenig" <ark@acm.org>
Date: Wed, 6 Dec 2006 09:39:12 CST
Raw View
"John Nagle" <nagle@animats.com> wrote in message
news:pvpdh.19462$9v5.10796@newssvr29.news.prodigy.net...

> This is valid, but requires typedefs.
> Can this return type be written in a single statement?
> Or does that exceed the capabilities of the declaration syntax?

> typedef char array3[3];
> typedef array3& array3ref;

> array3ref foo();

charr (&foo())[3];

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "t.y.c." <tuyongce@gmail.com>
Date: Wed, 6 Dec 2006 09:38:29 CST
Raw View

On Dec 6, 2:38 pm, John Nagle <n...@animats.com> wrote:
> This is valid, but requires typedefs.
> Can this return type be written in a single statement?
> Or does that exceed the capabilities of the declaration syntax?
>
>         typedef char array3[3];
>         typedef array3& array3ref;
>
>         array3ref foo();
>
You can do it as follow:
typedef char (&array3ref)[3];

But I don't think this type is useful ^_^  , array3& is enough

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: James Dennett <jdennett@acm.org>
Date: Wed, 6 Dec 2006 10:06:47 CST
Raw View
John Nagle wrote:
> This is valid, but requires typedefs.
> Can this return type be written in a single statement?
> Or does that exceed the capabilities of the declaration syntax?
>
>     typedef char array3[3];
>     typedef array3& array3ref;
>
>     array3ref foo();

Looks nothing special to me, beyond the usual pathological
nature of C++ declaration syntax:

char array[3];

char (&foo())[3]
{
  return array;
}

This compiles for me with Comeau C++ 4.3.3, and with g++
3.3.  (Of course it's clearer with suitable typedefs, and
this is rather an odd case to want anyway, but the syntax
allows it.)

-- James

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "James Kanze" <james.kanze@gmail.com>
Date: Wed, 6 Dec 2006 10:08:36 CST
Raw View
John Nagle wrote:
> This is valid, but requires typedefs.
> Can this return type be written in a single statement?
> Or does that exceed the capabilities of the declaration syntax?

>  typedef char array3[3];
>  typedef array3& array3ref;

>  array3ref foo();

    char (&foo())[ 3 ] ;

As usual, you start with what you want to declare: "foo".  It's
a function, so "foo()", which returns a reference "&foo()" to
an array "&foo()[3]"---but the precedance is wrong, so we need
parentheses "(&foo())[3]".  And finally, char.

I'll admit that it looks a little strange as a definition:

    char (&foo())[ 3 ]
    {
        //  ...
    }

Make it a const member function, and have the array contain
pointers to member functions, and the syntax can become
downright difficult.  Something like the following (which
declares a function taking an int as a parameter, and not a
double):

    void (Bar::*(&Bar::foo( int i ) const)[ 3 ])( double d )

(To read, at least.  Curiously, I find it easier to write the
declarations.  But what good is it to write such declarations,
if you don't understand what they mean when you later read the
code?)

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]