Sign in to follow this  
David Neubelt

compiler errors on different compilers

Recommended Posts

The code below compiles with visual studio 2003/5, and gcc 2.3 but does not compile with SNC or the comeau compiler. Anyone know any work arounds?

#include <stdio.h>

class Foo {
};

class CKey {
public:

    template<class TX> static bool IsType(TX& r) {
        return true;
    }
    void foo() {
        printf("");
    }
};

template<class T> void Check(T& r) {
    CKey::IsType<T>(r);             // works
    CKey::IsType<CKey>(r);

    T::IsType<CKey>(r);             // doesnt work
    T::IsType<T>(r);                // doesnt work

    // -- doesnt work
    r.IsType<T>(r);         
    r.IsType<CKey>(r);
    r.IsType<CKey>(r);
    r.IsType<T>(r);
    
    // -- works
    r.foo();
}

int main() {
    CKey r;
    Check<CKey>(r);
    return 0;
}

Share this post


Link to post
Share on other sites
Welcome to one of the uglier corners of the C++ language:

T::template IsType<CKey>(r);
T::template IsType<T>(r);

r.template IsType<T>(r);
r.template IsType<CKey>(r);
r.template IsType<CKey>(r);
r.template IsType<T>(r);

Share this post


Link to post
Share on other sites
In order to parse the member templates of dependent typenames as templates the template keyword is used so that the compiler knows that the <>s are template argument delimiters rather than less than and greater than operators.

Share this post


Link to post
Share on other sites
That's a new one to me. I don't have a copy of the standard on hand. Regarding the fact VC++ compiles this and some other compilers don't, is that a non-standard extension in VC++, or is the compiler allowed to detect and handle these cases, but some others just aren't smart enough?

Share this post


Link to post
Share on other sites
Quote:
Original post by Nemesis2k2
That's a new one to me. I don't have a copy of the standard on hand. Regarding the fact VC++ compiles this and some other compilers don't, is that a non-standard extension in VC++, or is the compiler allowed to detect and handle these cases, but some others just aren't smart enough?


If you do grab a copy, look at section 14.6. A program that doesn't use typename as appropriate is ill-formed. This being a syntactic rule, conforming C++ implementations "shall" issue at least one diagnostic message, as it is a diagnosable rule (see 1.4, specifically sections 2 and 1).

If my hazy memory serves me right, the trend has been away from handling these non standard cases -- GCC started issuing missing typename warnings, then later errors. The situation had an ironic reversal when it came to the typename keyword at least at one point, where VS2005 would issue errors when the template keyword was missing as per my obtuse example above, where GCC would ignore these issues outright. This lead to some rather nasty error counts when it came time to port a month's worth of template hackery developed exclusively on GCC to VS2005.

Moral: Build your code frequently on both compilers if you're working on portable template evil.

Share this post


Link to post
Share on other sites
By my rough estimate, about 60% of chapter 14 would just disappear if C++ used some other mechanism of indicating template arguments instead of <>. For example: <: :>.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
By my rough estimate, about 60% of chapter 14 would just disappear if C++ used some other mechanism of indicating template arguments instead of <>. For example: <: :>.
I just couldn't resist the urge to suggest we all use smiley faces instead (: :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this