struct A
{
template<int>
void foo()
{}
};
int main()
{
A a;
a.foo<0>(); // ok
a.template foo<0>(); // also ok
}
Obviously, a.foo<0>();
is more concise, intuitive, and expressive than a.template foo<0>();
.
Why does C++ allow a.template foo<0>();
even though a.foo<0>();
is enough?
Sometimes, inside a template, you need to write a.template foo<0>()
instead of a.foo<0>()
.
@melpomene gave this great example in the comments:
template<typename T>
void do_stuff() {
T a;
a.template foo<0>();
}
do_stuff<A>();
a.template foo<0>()
should not be used in your current situation.g++
would output the following warning when compiling your code:
warning: 'template' keyword outside of a template [-Wc++11-extensions]
a.template foo<0>()
syntax everywhere.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