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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With