c++gcc

Template metaprogramming GCC error


Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?

There appears to be a bug in GCC 4.5.3:

#include <type_traits>

template <bool isFundamentalType, bool isSomething>
struct Foo
{
    template <typename T>
    static inline void* Do(size_t size);
};

template <>
struct Foo<true, false>
{
    template <typename T>
    static inline void* Do(size_t size)
    {
        return NULL;
    }
};

template <>
struct Foo<false, false>
{
    template <typename T>
    static inline void* Do(size_t size)
    {
        return NULL;
    }
};

class Bar
{
};

template <typename T>
int Do(size_t size)
{
    // The following fails
    return (int) Foo<std::is_fundamental<T>::value, false>::Do<T>(size);
    // This works -- why?
    return (int) Foo<false, false>::Do<T>(size);
}

int main()
{
    return Do<Bar>(10);
}

Compiled with g++ bug.cpp -std=c++0x

Errors:

bug.cpp: In function ‘int Do(size_t)’:
bug.cpp:37:65: error: expected primary-expression before ‘>’ token

Is there a known workaround that would allow me to side step this issue?

EDIT: MSVC 2010 managed to compile this just fine.


Solution

  • You need to add template:

    return (int) Foo<std::is_fundamental<T>::value, false>::template Do<T>(size);
    

    MSVC 2010 compiles the code because it doesn't handle templates correctly.

    Side Note

    MSVC also injects size_t into the global namespace because of a long withstanding bug. Technically you need to include the proper header on other compilers.