sfinae for g++ 4.1.2

182 Views Asked by At

I have a problem with SFINAE with G++ 4.1.2.

The following code works properly for 4.6:

#include <stdio.h>

class Test
{
public:
    int x;
};

template <typename T> 
inline T f(T v) { return v;}                // Definition #2

template <typename T> 
inline typename T::x f(T v) { return v.x; } // Definition #1

int main()
{
Test t;
t.x = 100;

    printf("Test.x = %d\n", f(t));
    printf("int = %d\n", f(10));
}

Naturally, the output is:

Test.x = 100
int = 10

I need something like to this to work on g++ 4.1.2, any ideas ? Frankly, I don't understand how there could be so much difference between these two compilers!

Or if you have an alternative for it, that would be great. Something with templates! Or MACROS...

1

There are 1 best solutions below

1
On BEST ANSWER

I actually solved a part of my problem...

But for this case here, it should be template<class T> and not template<typename T>...

So just an half Hi-Five to myself. But for the second part, I used another approach, it was annoying to do, but it worked.