I have a templated class which contains a static function which does not depend on the template parameter. Yet the compiler seems to force me to specify a typename when I use the function.
template <typename T>
class MyClass {
...
static void function();
};
template <typename T>
void MyClass<T>::function() {
....
}
This function can be used as:
MyClass<int>::function();
But the ‘int’ is just there to satisfy the compiler. It doesn’t mean anything, and can be replaced by any other type, which doesn’t increase code readability. I would like to do someting like
MyClass<>::function();
or even
MyClass::function();
but the compiler doesn’t let me. I realize that this is because in the header files I have explicitly marked the function as being templated, but when I remove the ‘< T >’ from the header file it doesn’t compile either.
What is the correct way to do this?
Imagine you had this:
Now imagine what happens if you say
Foo<int>::boom(); Foo<int>::boom();, and compare that to the not-at-all equivalentFoo<int>::boom(); Foo<char>::boom();.