Topic: function
Author: joao sampaio <j.sampaio@ip.pt>
Date: 1999/06/23 Raw View
Siemel Naran wrote:
> Third, the ability to declare a function inside a function means
> that the following is a syntax error:
> struct Thing { };
> int main() { Thing t(); } // VERSION1
> We'd like this to mean: declare a variable 't' of type Thing. That
> is, the same as
> int main() { Thing t; } // VERSION2
> However, what VERSION1 really means is: declare a function 't' that
> takes no arguments and returns a Thing by value. Whew!
This would have been a problem also without the ability to declare a
function inside another one. Thing t(); when declaring a global object, also
declares function t() and not object t.
Joao
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/06/18 Raw View
Arun Thomas wrote:
>
> Can u prototype a function within another function? I don't think you
> can, and I don't know why you'd want to. Nevertheless, I'm not sure, and
Yes, it's legal. You might want to do it for the same reason you might
want to put any declaration inside a function rather than outside. That
is, to restrict the scope of the prototype to that function, so that you
can't accidentally use it in other functions defined in the same
tranlsation unit. I haven't seen a good use for it, but it's a valid
technique.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/06/19 Raw View
James Kuyper <kuyper@wizard.net> writes:
>Arun Thomas wrote:
>>
>> Can u prototype a function within another function? I don't think you
>> can, and I don't know why you'd want to. Nevertheless, I'm not sure, and
>Yes, it's legal. You might want to do it for the same reason you might
>want to put any declaration inside a function rather than outside. That
>is, to restrict the scope of the prototype to that function, so that you
>can't accidentally use it in other functions defined in the same
>tranlsation unit. I haven't seen a good use for it, but it's a valid
>technique.
It's valid in the sense of being legal code. It's a bad idea in
C++, however, since it affects overloading. Example:
extern int g(double); // from some included header
int f(double x)
{
extern int g(int);
return g(x); // what happens here?
}
Given this code g(int) will be called instead of g(double),
because the local declaration of g hides all outer
declarations of g.
Moving a declaration into or out of a function body could result
in a different function being called, leading to obscure
changes in program behavior.
If for some reason you really wanted to call g(int), this
is not the right way to do it. Use a cast instead:
g( static_cast<int>(x) );
To avoid inconsistent overloading resolution, you should not
generally have free function declarations. Declare functions,
other than static functions local to a translation unit, in
headers, grouping related declarations together in the same header.
--
Steve Clamage, stephen.clamage@sun.com
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Arun Thomas <at4a@virginia.edu>
Date: 1999/06/16 Raw View
Can u prototype a function within another function? I don't think you
can, and I don't know why you'd want to. Nevertheless, I'm not sure, and
I am very curious about this.
Thanks
Arun
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: wmm@fastdial.net
Date: 1999/06/17 Raw View
In article <3767E050.F8885B@virginia.edu>,
Arun Thomas <at4a@virginia.edu> wrote:
> Can u prototype a function within another function? I don't think you
> can, and I don't know why you'd want to. Nevertheless, I'm not sure,
and
> I am very curious about this.
If I understood correctly, the answer is "yes." For example,
void f() {
void g(); // declare function "g"
g(); // and call it
}
You can also have block-scope object declarations with linkage:
void f() {
extern int i;
}
See 3.5p6 of the Standard.
It's not clear to me that it's particularly useful, though.
--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/06/17 Raw View
On 16 Jun 99 18:08:08 GMT, Arun Thomas <at4a@virginia.edu> wrote:
>Can u prototype a function within another function? I don't think you
>can, and I don't know why you'd want to. Nevertheless, I'm not sure, and
>I am very curious about this.
Yes, you can. And I have no idea why C++ allows you to do this.
First of all
// f.h
void f(int x) { int g(int); cout << g(2*x) << '\n'; }
// g.c
int g(int x) { return 2*x; }
// main.c
#include "f.h"
int main() { f(3); } // prints "12"
Sure, by declaring function g(int) inside function f(int), you gain
extreme locality of reference. OTOH, the preferred style of declaring
functions is to include the function's header file. Hence, this is
better:
// g.h
int g(int);
// f.h
void f(int x) { cout << g(2*x) << '\n'; }
// g.c
#include "g.h"
int g(int x) { return 2*x; }
// main.c
#include "f.h"
int main() { f(3); } // prints "12"
This is better because
you get to choose what namespace function g resides in
you don't have to duplicate function signatures -- eg, later you may
have two funcitons f1 and f2 that need to use g
if you add overloaded functions in file g.h, then files f.h takes
the overload into account
Second, declaring a function inside a function may intefere with
overloading. Eg,
namespace Silly {
double g(double);
int g(int);
void f(int x) {
void g(double);
cout << g(2*x) << '\n'; // calls f(double)
}
}
Just looking at g(2*x), we expect the compiler to call f(int), but
instead it calls f(double). This is unituitive. However, if class
double were a member of namespace Silly, then the compiler would
call Silly::g(int) anyway.
Third, the ability to declare a function inside a function means
that the following is a syntax error:
struct Thing { };
int main() { Thing t(); } // VERSION1
We'd like this to mean: declare a variable 't' of type Thing. That
is, the same as
int main() { Thing t; } // VERSION2
However, what VERSION1 really means is: declare a function 't' that
takes no arguments and returns a Thing by value. Whew!
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James.Kanze@dresdner-bank.com
Date: 1999/06/17 Raw View
In article <3767E050.F8885B@virginia.edu>,
Arun Thomas <at4a@virginia.edu> wrote:
> Can u prototype a function within another function? I don't think you
> can, and I don't know why you'd want to. Nevertheless, I'm not sure,
and
> I am very curious about this.
You can. I'm not sure why you'd want to do this either, but C allowed
it, and there is no overriding reason for C++ to differ here.
--
James Kanze mailto:
James.Kanze@dresdner-bank.com
Conseils en informatique orient=E9e objet/
Beratung in objekt orientierter
Datenverarbeitung
Ziegelh=FCttenweg 17a, 60598 Frankfurt, Germany Tel. +49 (069) 63 19 86
27
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Pete Becker <petebecker@acm.org>
Date: 1999/06/17 Raw View
Siemel Naran wrote:
>
> On 16 Jun 99 18:08:08 GMT, Arun Thomas <at4a@virginia.edu> wrote:
>
> >Can u prototype a function within another function? I don't think you
> >can, and I don't know why you'd want to. Nevertheless, I'm not sure, and
> >I am very curious about this.
>
> Yes, you can. And I have no idea why C++ allows you to do this.
It's because C allows it. Older C code is riddled with this sort of
thing.
--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: wmm@fastdial.net
Date: 1999/06/17 Raw View
In article <slrn7mghrc.g8b.sbnaran@localhost.localdomain>,
sbnaran@uiuc.edu wrote:
> On 16 Jun 99 18:08:08 GMT, Arun Thomas <at4a@virginia.edu> wrote:
>
> >Can u prototype a function within another function? I don't think you
> >can, and I don't know why you'd want to. Nevertheless, I'm not sure,
and
> >I am very curious about this.
>
> Yes, you can. And I have no idea why C++ allows you to do this.
Because disallowing it would have been a gratuitous incompatibility
with C, which also allows it. (Which pushes the question back by
one language, but still doesn't answer it. :-)
--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Pete Becker <petebecker@acm.org>
Date: 1999/06/17 Raw View
wmm@fastdial.net wrote:
>
>
> Because disallowing it would have been a gratuitous incompatibility
> with C, which also allows it. (Which pushes the question back by
> one language, but still doesn't answer it. :-)
>
But it does move it to a different newsgroup, so it's out of our
inboxes. Phew!
--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]