469,160 Members | 1,877 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

static_cast and globally scoped types.

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

--
Otto Lind Kabira Technologies (http://www.kabira.com)
ot**@olcs.com 12193 285th street, Lindstrom, MN 55045
Jul 22 '05 #1
4 1633
On 05 Mar 2004 19:43:14 GMT, ot**@olcs.com (Otto Lind) wrote:
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
le**@bdsoft.com
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
Jul 22 '05 #2
Otto Lind wrote:
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);

--
++acr@,ka"
Jul 22 '05 #3
On Fri, 05 Mar 2004 20:08:02 GMT, Leor Zolman <le**@bdsoft.com> wrote:
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
le**@bdsoft.com
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
Jul 22 '05 #4

"Sam Dennis" <sa*@malfunction.screaming.net> a écrit dans le message de
news:sl****************@ID-227112.user.uni-berlin.de...
Otto Lind wrote:
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
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Scott Brady Drummonds | last post: by
26 posts views Thread by Steven T. Hatton | last post: by
3 posts views Thread by shrishjain | last post: by
9 posts views Thread by Vincent RICHOMME | last post: by
3 posts views Thread by Rahul | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.