O
Otto Lind
The following program shows the problem:
#include <stdio.h>
namespace x
{
enum L { One = 1, Two = 2 };
namespace x
{
enum L { Three = 3, Four = 4 };
void foo(long mylong)
{
::x::L l;
//
// This fails to compile due to syntax error
//
l = static_cast<::x::L>(mylong);
//
// This (correctly) fails to compile since it uses
// wrong enum.
//
l = static_cast<x::L>(mylong);
}
}
}
int main()
{
long l = 1;
x::x::foo(l);
return 0;
}
When compiled with g++ (3.3.2) and Sun's CC (5.5 Patch 113817-03), both
report a syntax error when using a globally scoped type:
% CC foo.cpp
"foo.cpp", line 19: Error: "<" expected instead of "<:".
"foo.cpp", line 19: Error: Type name expected instead of "<:".
"foo.cpp", line 19: Error: Expected an expression.
"foo.cpp", line 24: Error: Cannot assign x::x::L to x::L.
% g++ foo.cpp
foo.cpp: In function `void x::x::foo(long int)':
foo.cpp:19: error: parse error before `[' token
foo.cpp:24: error: cannot convert `x::x::L' to `x::L' in assignment
I had assumed that globally scoped types could be used in static_cast<>,
specifically to resolve scope issues. Is this a bug in the compiler? If
not, what would be the best workaround?
Otto
#include <stdio.h>
namespace x
{
enum L { One = 1, Two = 2 };
namespace x
{
enum L { Three = 3, Four = 4 };
void foo(long mylong)
{
::x::L l;
//
// This fails to compile due to syntax error
//
l = static_cast<::x::L>(mylong);
//
// This (correctly) fails to compile since it uses
// wrong enum.
//
l = static_cast<x::L>(mylong);
}
}
}
int main()
{
long l = 1;
x::x::foo(l);
return 0;
}
When compiled with g++ (3.3.2) and Sun's CC (5.5 Patch 113817-03), both
report a syntax error when using a globally scoped type:
% CC foo.cpp
"foo.cpp", line 19: Error: "<" expected instead of "<:".
"foo.cpp", line 19: Error: Type name expected instead of "<:".
"foo.cpp", line 19: Error: Expected an expression.
"foo.cpp", line 24: Error: Cannot assign x::x::L to x::L.
% g++ foo.cpp
foo.cpp: In function `void x::x::foo(long int)':
foo.cpp:19: error: parse error before `[' token
foo.cpp:24: error: cannot convert `x::x::L' to `x::L' in assignment
I had assumed that globally scoped types could be used in static_cast<>,
specifically to resolve scope issues. Is this a bug in the compiler? If
not, what would be the best workaround?
Otto