[Qt-interest] Weird compiler error with QString(QChar(value))
Thiago Macieira
thiago at kde.org
Fri May 7 14:13:26 CEST 2010
Em Sexta-feira 07 Maio 2010, às 11:19:36, Srdjan Todorovic escreveu:
> Hi,
>
> On 6 May 2010 19:39, K. Frank <kfrank29.c at gmail.com> wrote:
> > There is a good explanation for this in Marshall Cline's "C++ FAQ LITE":
> > http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19
>
> I just used that as a template for my own test snippet.
>
> Questions, no doubt off-topic for Qt:
>
> 1) Why does the C++ compiler / standard allow function declarations
> inside a function?
> (as opposed to the C-style declarations outside functions that I
> have encountered before)
Heritage from C, I guess.
You can declare external functions inside a function, which means they are
available in that context only. So:
void foo()
{
extern void bar();
bar();
}
void baz()
{
bar();
}
Will generate an error in baz, because bar is not declared. That seems to be
the only reason for placing it inside the function.
> Surely this is not related to function pointers. Or is it?
No.
> Also does a function declaration inside a function go out of
> scope after the function
> hits the ret asm instruction? Does it ever go in scope if the
> function is *never* called?
See above.
> Note that definitions are not allowed (in GCC) and that seems to
> make sense.
>
> 2) Does anyone ever use this feature of C++?
You can find them in a couple of places in Qt code. It usually happens that you
need a function from another source file, so you declare the reference to it at
the point you need.
Also note the "extern" keyword in my example: it's a no-op in that context.
The "extern" keyword is one of the three mostly-useless keywords in C++98, but
unlike the other two, it does have an existing meaning. Outside a function,
these declarations are not the same in C++:
int i;
extern int i;
The first declares a variable called 'i' and exports that symbol; the second is
simply using the variable 'i' from another context. Interestingly, they are
the same in C, due to "common symbols".
Exercise for the reader: what are the other two keywords that are mostly
useless in both C++98 and C99?
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Senior Product Manager - Nokia, Qt Development Frameworks
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100507/a582a576/attachment.bin
More information about the Qt-interest-old
mailing list