static_cast and globally scoped types.

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
 
L

Leor Zolman

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);

Looks like <: is a diagraph equivalent of '[', believe it or not (I didn't
know that...). Put a space after the <.
//
// This (correctly) fails to compile since it uses
// wrong enum.
//
l = static_cast<x::L>(mylong);
right.
-leor


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
S

Sam Dennis

Otto said:
l = static_cast<::x::L>(mylong);
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:

"foo.cpp", line 19: Error: "<" expected instead of "<:".

Your problem is not the same as the problem you think it is; `<:' is the
digraph for `[' (in C, at least, and I would think that this is the same
in C++), so you do, indeed, have a syntax error.

Try this, instead:

l = static_cast< ::x::L >(mylong);
 
L

Leor Zolman

Looks like <: is a diagraph equivalent of '[', believe it or not (I didn't
know that...). Put a space after the <.
And yes, I /do/ know how to spell "digraph". That was Freudian slip of the
fingers.
-leor

Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
J

jacob navia

Sam Dennis said:
Otto said:
l = static_cast<::x::L>(mylong);
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:

"foo.cpp", line 19: Error: "<" expected instead of "<:".

Your problem is not the same as the problem you think it is; `<:' is the
digraph for `[' (in C, at least, and I would think that this is the same
in C++), so you do, indeed, have a syntax error.

Try this, instead:

l = static_cast< ::x::L >(mylong);

--

I have proposed in comp.std.c that WE FORGET this digraph/trigraph
nonsense...
I can't understand that C++ needs to be bug compatible with C.

How many "incredible" bugs we will endure?

In C this is less of a problem than in C++, where <: is much more common!

jacob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,148
Latest member
ElizbethDa
Top