[BUG] ICE when using templated user-specified cast operator

D

Dan Plimak

Greetings,

I'm seeing an odd problem while implementing a template user-specified cast
operator for one of my objects which frequently gets static_cast<>ed around.

The following test case demonstrates the problem, producing an Internal
Compiler Error on the line marked. I'm fairly sure this is valid C++, but I
could be wrong. (g++ at least, chows down on this without complaint.)

--- snip ---
#include <stdio.h>

class Foo
{
public:

template <typename _T> operator const _T*() const
{
return static_cast<const _T*>(this);
}
};

class FooX : public Foo
{
};

int main()
{
FooX f;

const FooX *foox = &f;
const Foo *foo = *foox; // boom!

return 0;
}
--- snip ---

Any ways around this would be appreciated, as are beatings around the head
for missing some obvious C++ legalism. :)

Cheers,

-- Dan
 
D

Dan Plimak

Oh, and I neglected to mention that I'm using VC++ 7.1, aka VS .NET 2K3.

Cheers,

-- Dan
 
D

David Lowndes

I'm seeing an odd problem while implementing a template user-specified cast
operator for one of my objects which frequently gets static_cast<>ed around.

The following test case demonstrates the problem, producing an Internal
Compiler Error on the line marked. I'm fairly sure this is valid C++, but I
could be wrong. (g++ at least, chows down on this without complaint.)

Dan,

I don't know whether the code is in any way suspect - it looks
reasonable to me, but a fairly reliable test is the online Comeau
compiler which also seems to think it's OK.

The ICE reproduces with the Whidbey compiler too, so I'll report it to
MS.

Dave
 
D

Dan Plimak

I've been fiddling with trying to get the compiler to accept it, but it
looks like no amount of de-constifying, adding random keywords like 'inline'
makes this ICE go away. Looks like it's due to a fundamental inability to
handle this type of template scenario, and it's back to doing static_cast<>s
all over the place for me...

Mind you, the below scenario does get accepted, but my actual code -- which
uses the same template construct, but somewhat more levels of inheritance --
still causes the compiler to bomb.

class Foo
{
public:

template <typename _T> operator const _T*() const
{
return static_cast<const _T*>(this);
}
};

class FooX : public Foo
{
};

int main()
{
FooX f;

const Foo *foo = &f;
const FooX *foox = *foo;

return 0;
}

-- Dan
 
B

Bo Persson

Dan Plimak said:
Oh, and I neglected to mention that I'm using VC++ 7.1, aka VS .NET
2K3.

You are not allowed to use names starting with the combination of
underscore and uppercase letter. Those names are all reserved to the
implementer. Microsoft has already used _T as macro name in tchar.h !


Bo Persson
 

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

Top