[cfe-dev] Why this extern "C" fails ?
David Chisnall
David.Chisnall at cl.cam.ac.uk
Tue Jul 30 01:08:39 PDT 2013
On 30 Jul 2013, at 06:43, Arji Cot <arjicot at gmail.com> wrote:
> 1 ) this works flawlessly, and as far as I know C and C++ have the same rules for scoping
>
> #include <iostream>
>
> int main() {
> {
> int a = 42;
> { std::cout << a << "\n"; }
> }
> return (0);
> }
This does not have the same scoping as this:
> #include <iostream>
>
> int main() {
> {
> extern "C" { int a = 42; }
> { std::cout << a << "\n"; }
> }
> return (0);
> }
The equivalent is:
#include <iostream>
int main() {
{
{ int a = 42; }
{ std::cout << a << "\n"; }
}
return (0);
}
The scope of a is the block in which it is declared, so it is not in scope by the time it is used.
Perhaps your original example was meant to be:
#include <iostream>
int main() {
{
extern "C" int a = 42;
{ std::cout << a << "\n"; }
}
return (0);
}
This also fails to compile. I'm not sure what you'd expect the semantics to be. The variable a has automatic storage, and so a linkage specifier makes no sense in this context.
David
More information about the cfe-dev
mailing list