Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I pass template parameters on to another template?

Okay I'm going to give a simple example of my problem:

void Increment(Tuple<int, int>& tuple) {
    ++tuple.Get<0>();
}

int main() {

    Tuple<int, int> tuple;

    tuple.Get<0>() = 8;

    Increment(tuple);

    printf("%i\n", tuple.Get<0>()); // prints 9, as expected

    return 0;

}

This compiles just fine, and all is peachy. The Increment function just increments the first element in the tuple, and then I print that element. However, wouldn't it be nice if my Increment function could be used on any kind of element?

template <typename T>
void Increment(Tuple<T, T>& tuple) {
    ++tuple.Get<0>(); // <-- compile ERROR
}

int main() {

    Tuple<int, int> tuple;

    tuple.Get<0>() = 8;

    Increment<int>(tuple);

    printf("%i\n", tuple.Get<0>());

    return 0;

}

My second example spits out the following error at compile-time:

error: expected primary-expression before ')' token

I'm at my wits end trying to figure out why this causes problems. Since the template parameter is 'int', the generated code should be identical to my hard-coded example. How can I get this to work?

like image 861
nonoitall Avatar asked Dec 30 '10 10:12

nonoitall


1 Answers

It should be:

++tuple.template Get<0>();

In the same way you need typename to specify a type qualified from a dependent type, you need template to specify a template function qualified from a dependent type.

like image 184
GManNickG Avatar answered Nov 15 '22 11:11

GManNickG