The default template argument for class functions: where to specify it?

Where should I specify the default template parameters for member functions of classes (assuming that the declaration (of course) is in the "body class", and the definition of the function is outside the class body) for each case in C ++ 2011:

  • "normal" functions
  • static functions
  • friend functions

In the definition, in the ad or both?

+4
source share
2 answers

Well, From my experience in creating template classes and methods, you specify a template function as such:

template<typename T> T MyFunc(T &aArg1, T &aArg2) { //...Definition Goes Here } 

typename T is the type of the template argument for the template function, and you need to pass this data type sequentially to each argument marked as "T". This means that aArg2 must be any data type aArg1. Now, when you call this function, you call it like this: MyFunc</*datatype*/int>(iArg1, iArg2); two arguments must be an int data type or you will get a warning or error.

Now this also applies to class methods (I think this is what you mean by “class member functions”), which are functions provided by the class (i.e. MyClass::MyFunc() ), so when you declare a method class, which is a template, you do it the same way. Here is an example class:

 class MyClass { MyClass(); ~MyClass(); template<typename T> static T MyStaticFunc(T aArg) { return aArg; } template<typename T> T MyFunc(T aArg) { return aArg; } } 

As you can see, it’s not difficult. Now static functions are similar to how you should be sure that t will be defined then in the same module into which the class is built, otherwise you will receive an error message.

Unfortunately, I never use friend methods, so I don’t know how to solve this. I would suspect that you will do it the same way as the other two. I was hoping that all of the essay answer helped.

+1
source

Attempting these actions in Clang suggests the following:

  • For static and static functions that indicate the default value in a definition, or a declaration is acceptable - but not both and, of course, not if they contradict each other;
  • For friend functions that define a default value in the class definition, an error occurs.
0
source

All Articles