Where do I have to specify default template parameters of classes member functions (assuming that the declaration is (of course) in the “class body”, and the function definition is outside the class body) for each case in C++2011 :
- “normal” functions
- static functions
- friend functions
In the definition, in the declaration or both ?
Well,
From my experiences creating template classes and methods, you specify a template function as such:
The
typename Tis the template argument type for the template function and you need to pass that data type consistently to each argument labeled as “T”. This means that aArg2 has to be whatever data type aArg1 is. Now, when you call this function, you call it like so:MyFunc</*datatype*/int>(iArg1, iArg2);the two arguments have to be data type “int” or you’ll get a warning or an error.Now, this also applies to class methods (I think that is what you meant by “classes member functions”) which are the functions supplied by the class (i.e.
MyClass::MyFunc()) so when you declare a class method that is a template method, you do it in the same manner. Here is an example class:As you can see, not to difficult. Now, static functions are the same way you just have to be sure t define then in the same module that the class is built in, otherwise, you’ll get an error.
Unfortunately, I never really use “friend” methods, so I don’t know how to tackle that. I would suspect you would do it in the same way as the other two. I hoped that whole essay of an answer helped.