Tricky template question

Hi members,

I have a tricky (at least to me) question about templates.

I am defining a template called Tuple that looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// File Tuple.h

template< typename Type, size_t Size >
class Tuple
{
public:

    Tuple ();

    ~Tuple ();

    // [...]
    // I have defined copy constructor and affectation operator also,
    // and they work
    // [...]

    template< typename SubType, size_t Start, size_t SubSize >
    Tuple< SubType, SubSize > sub () const;

protected

    Type* mPtr;
};

template< typename Type, size_t Size >
Tuple< Type, Size >::Tuple ()
{
    if (Size)
        mPtr = new Type [Size];
    else
        mPtr = NULL;
}

template< typename Type, size_t Size >
Tuple< Type, Size >::~Tuple ()
{
    if (mPtr)
        delete mPtr;
    mPtr = NULL;
}

template< typename Type, size_t Size >
template< typename SubType, size_t Start, size_t SubSize >
Tuple< SubType, SubSize > Tuple< Type, Size >::sub () const
{
    assert(Start <= Size && Start+SubSize <= Size);
    Tuple< OutType, SubSize > result;
    for (int i = 0; i< SubSize; ++i)
    {
        result.mPtr[i] = mPtr[i+Start];
    }
    return result;
}



This works flawlessly with visual studio's compiler, but doesn't with gcc ...

Whenever I try to call sub, like in:

1
2
Tuple< float, 5 > lA;
Tuple< float, 3 > lB = lA.sub<float, 2, 3>();


it seems like g++ interprets the '<' character after "sub" as a comparison operator and not as the tmplate parameter list's start ...

Is there a way to do something like this somehow ?

Thanks,
Florent.
when you are accessing template members of a template class, you must add template to make sure that < is parsed as start of template instead of less-than comparison:
Tuple< float, 3 > lB = lA.template sub<float, 2, 3>();
Wow ! That was fast !

Thanks a bunch, i'll try that ASAP.
Topic archived. No new replies allowed.