confused with template class in C++ - c++

I know that the template class definitions is like:
template <class TYPE>
class cars{
public:
TYPE myCar;
}
but somewhere I encountered to this piece of code:
template <class T>
class polynomialT {
...
}
**************************************
class GFNUM2m {
...
}
**************************************
template class polynomialT<GFNUM2m>;
the last line is vague for me? any one knows what's up? is it an object of polynomialT class?(it seems not because it has no name) is it template?(it seems a duplicate because it has been templated once)

template class polynomialT<GFNUM2m>;
Is a request to explicitly instantiate the template class polynomialT with GFNUM2m, including instantiating all its non-template functions.
Some cases when this is needed are:
When you want to avoid multiple template instantiation (that then get purged by the linker);
When you want to make sure that the full template can be instantiated even for those (non-template) functions not called;
When you want to provide template definitions within a .cpp file;

the last line is equivalent to:
class polynomialT {
protected:
GFNUM2m *coeff; // array of coefficients (? see below)
int degree;
...
}
GFNUM2m *coeff is not an array, is simply a pointer to a GFNUM2m variable. Array and pointer are linked in some way, for example you can allocate dynamically an array with coeff = new GFNUM2m[10], but it is discouraged.

In fact, it is _explicit template instantiation. Use this to get the compiler to generate all (non-nested-template) members of the template class. This is convenient sometimes when linking externally to templated code, to prevent duplication of object code or missing externals (when methods get inlined).
Template specializations seem similar, but require template<> to announce the specialization of an already-declared template. Also, they would define an alternative class definition for that specific template parameter (as #rerun mentions).
Now, on the crosspoint of those, you could see
template<> class polynomialT<GFNUM2m>;
Which IS, in fact, a forward declared template specialization. This would serve to prevent the compiler from auto-instantiating the class template for that type parameter during the rest of the translation unit.

The last line is a forward declaration of the polynomialT class template with a template parameter of GFNUM2m, which also instantiates the template class.

it means template in the class polynomialT is now GFNUM2m class.

Related

function template with non-type parameters

I am very new on C++ templated code. could you please educate me on the following questions:
If I want to use function template (that only has non-type parameters) to define member function for a class, do I also need to define template for that class too. or in other word, does C++ allow user to define member function template (that has only non-type parameters) within untemplated class. for instance:
class foo{
template <int vecsize>
void bar(std::array<int, vecsize> vec);
};
thank you.
Yes, member function templates don't require the parent structure to be templatized (but it could be a template of course).
It seems you want a std::array instead of std::vector (as n.m. mentioned there is no size parameter for std::vector).
And you're missing a semicolon at the end of the class definition

How do template member functions work in c++?

I'm wondering how template member functions work. In particular, when there is an instantiation of the template member function, is the whole class redefined? My confusion comes from the fact that (if I'm right) template classes are not classes in the proper sense. i.e., when instantiated, the compiler creates the definition for a completely new class. The same for template functions. However, classes with a template function seem to be actual classes, so I'm not sure how they could possibly work. Thus, I'm wondering, after instantiating a template member function, what happens with the class definition? Moreover, if I pass a class with a template member function to a template class, can I use the template member function? Could that cause a problem? I tried it once but got an error saying that several functions where defined more that once, although I'm not sure if that was the reason or if there could be an other reason for my error. Is there any further caveat when using static template member functions?
The class definition remains as it is; all the template function does is generate a family of member functions for that class. As an example:
class A {
public:
template<typename T> foo (T &t);
}
Is not conceptually different from you writing:
class A {
public:
foo (bool &t);
foo (int &t);
foo (double &t);
}
...just more convenient. And in the last example, you wouldn't expect a new class to be created for each function would you?
Perhaps the confusion comes from the notion that functions are somehow part of the memory layout of a class; that each function is itself contained in the class, and will be instantiated somewhere in memory whenever an object of the class is created. This notion is incorrect. Functions (templated, global, member, lambda, or otherwise) are never created on the fly or copied around in memory; they are a static and unchanging part of the executable image. The memory layout of the class is not changed by the presence of an extra set of functions, even if those happen to be generated by a template member.
The template class definition is instantiated when you instantiate a class. Each member function of it is instantiated when used. This actually allows you to have member functions that would not work if called when the class is instantiated with some types and not others. However, you must ensure that the signature of the function is either syntactically viable or fails with SFINAE. It will be looked up during the first phase of parsing. The body, if the function isn't itself a template, will be checked for name lookup...so dependent names have to be labeled as such via typename.

Default template argument for functions of classes : where to specify it?

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:
template<typename T>
T MyFunc(T &aArg1, T &aArg2)
{
//...Definition Goes Here
}
The typename T is 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:
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, 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.
Trying these out in Clang suggests the following:
For non-static and static functions, specifying the default in either the definition or
the declaration is acceptable - but not both and certainly not if
they contradict one another;
For friend functions, specifying a
default inside the class definition results in an error.

Template class in C++

We have following class definition
template<typename T>
class Klass {...}
and we also have below two instantiations
Klass<int> i;
Klass<double> d;
how many copies of Klass' methods are generated by the C++ compiler?
Can somebody explain it? Thanks!
Klass isn't a type, so it doesn't make sense to talk of Klass's methods. Kalss<int> is a type with it's own methods, and so is Klass<double>. In your example there would be one set of methods for each type.
Edit in real life, it isn't as simple as that. The question of the actual existence of the methods also depends on other factors, see #KerrekSB's answer to this question.
Each template instance is an entirely separate, distinct and independent type of its own. However, the code for class template member functions is only generated if the member function is actually used for a given template instantiation (unless you instantiate the template explicitly for some set of parameters). Among other things, this means that if some class template member function's body doesn't actually make sense for a given template parameter, then you can still use the overall template as long as you don't invoke that member function, since the code for the member function will never get compiled.
Also bear in mind that templates can be specialized:
template <typename T> struct Foo {
int bar;
void chi();
};
template <> struct Foo<int> {
double bar(bool, char) const;
typedef std::vector<bool> chi;
bool y;
};
As you can see, there's a lot you cannot just tell from a template itself until you see which actual instantiations you'll be talking about.

returning template types from non-template parameterized methods

To define a templated class, I consider three different files. The declaration is in a .h file, the methods implentations are in a .cpp file, and explicit instantiations are included in a .inc file (by adding a line at the end of the .cpp, e.g: #include "bar-impl.inc").
Now, here's my problem. I've two template classes, say: Foo<S> and Bar<T>.
Inside of the Bar<T> class, I've a method that returns a template Type FooType* ( which with my explicit instantiation I would like it to be, for example, Foo<float>*)
template<class T>
class Bar{
....
template <class FooType>
FooType* doSomething(int);
....
};
Since the compiler does not know what FooType* is, I tried to explicitly instantiated the doSomething method in the bar-impl.inc file.
//bar-impl.inc
template class Foo<float> * Bar<float>::doSomething(int);
However, it didn't work and I get an error of: no matching function for call to
‘Bar<float>::doSomething(int&)’
make: *** [main] Error 1
Does anybody knows if it's possible to do that?
Methods templates work exactly the same way as function templates. You need to explicitly instantiate them upon use unless the template parameters can be derived from the call.
So you don't really need to make a specialization, what you need to do is specify the FooType upon use:
Bar<float> somevar; somevar.doSomething< Foo<float> >(somevalue);
If you always want to return Foo<T>* then just use Foo<T>* doSomething(int);