Template Question

S

spyrosthalkidis

Dear Programmers,

I would like to extend the fplll library with an algorithm, so
I am currently reading its code. However, I do not completely
understand the use of templates at several places. I know the
basic use of function and class templates. I attach an example
code snippet:

template<class ZT, class FT>
MatGSO<ZT, FT>::MatGSO(Matrix<ZT>& argB, Matrix<ZT>& argU, Matrix<ZT>& argUInvT,
int flags) :
b(argB),
enableIntGram(flags & GSO_INT_GRAM),
enableRowExpo(flags & GSO_ROW_EXPO),
enableTransform(argU.getRows() > 0),
enableInvTransform(argUInvT.getRows() > 0),
rowOpForceLong(flags & GSO_OP_FORCE_LONG),
u(argU), uInvT(argUInvT),
nKnownRows(0), nSourceRows(0), nKnownCols(0),
colsLocked(false), allocDim(0)

It can be seen that this code defines a method which has two template classes
(ZT, FT) and has as parameter a matrix which is passed by reference and is
used as a ZT template.

The class MatGSO is defined as:

template<class ZT, class FT>
class MatGSO {

If anyone could help me I would be grateful.

Thank you in advance.

Spyros H.
 
Ö

Öö Tiib

Dear Programmers,

I would like to extend the fplll library with an algorithm, so
I am currently reading its code. However, I do not completely
understand the use of templates at several places. I know the
basic use of function and class templates. I attach an example
code snippet:

template<class ZT, class FT>
MatGSO<ZT, FT>::MatGSO(Matrix<ZT>& argB, Matrix<ZT>& argU, Matrix<ZT>& argUInvT,
int flags) :
b(argB),
enableIntGram(flags & GSO_INT_GRAM),
enableRowExpo(flags & GSO_ROW_EXPO),
enableTransform(argU.getRows() > 0),
enableInvTransform(argUInvT.getRows() > 0),
rowOpForceLong(flags & GSO_OP_FORCE_LONG),
u(argU), uInvT(argUInvT),
nKnownRows(0), nSourceRows(0), nKnownCols(0),
colsLocked(false), allocDim(0)

It can be seen that this code defines a method which has two template classes
(ZT, FT) and has as parameter a matrix which is passed by reference and is
used as a ZT template.

When talking about C++ we do not usually say "methods", we say "member
functions". However the above is start of something that we usually call
as "definition of constructor". Your snippet suddenly ends in middle of
"member initializer list". That list is not specific to templates, all
constructors have that. I typically format it bit differently:

template<class ZT, class FT>
MatGSO<ZT, FT>::MatGSO( Matrix<ZT>& argB, Matrix<ZT>& argU
, Matrix<ZT>& argUInvT, int flags)
: b( argB ) // initializer of MatGSO::b
, enableIntGram( flags & GSO_INT_GRAM )
, enableRowExpo( flags & GSO_ROW_EXPO )
// ... etc all bases/members in order of declaration.
{
// ... body of constructor (often empty)
}

Uhh ... I am not sure what you actually asked about it... ?
The class MatGSO is defined as:

template<class ZT, class FT>
class MatGSO {

You have problem that you post only start of something and name it as if
it was full definition. Somewhere in it should be the declaration of
contructor too (that you asked about) like:

MatGSO( Matrix<ZT>&, Matrix<ZT>&, Matrix<ZT>&, int );

.... or similar.
If anyone could help me I would be grateful.

You should start with learning basics about C++ syntax. Constructors,
destructors, member functions, operator overloading, things like that.
Do not jump into templates yet. If you want to extend libraries with
goodies from you then I feel that you are on skill level "unable to
without mentor".
 
S

spyrosthalkidis

Of course I know that the code snippets are not complete!
I did not want to delve into details!
What I want is someone to explain me how the
templates are used in these examples!
Are they class templates? Can two class templates
be joined as template<class A, class B> member_function<A,B>
and what does this mean?
Also of course I know that giving the same name as the class
name to a member function, means that it is a constructor!
I do not want someone to teach me C++! I want someone to explain
me the specific use of templates!

Spyros H.
 
S

spyrosthalkidis

To be more specific:

template<class ZT, class FT>
MatGSO<ZT, FT>::MatGSO(Matrix<ZT>& argB, Matrix<ZT>& argU, Matrix<ZT>& argUInvT,
int flags) :
b(argB),
enableIntGram(flags & GSO_INT_GRAM),
enableRowExpo(flags & GSO_ROW_EXPO),
enableTransform(argU.getRows() > 0),
enableInvTransform(argUInvT.getRows() > 0),
rowOpForceLong(flags & GSO_OP_FORCE_LONG),
u(argU), uInvT(argUInvT),
nKnownRows(0), nSourceRows(0), nKnownCols(0),
colsLocked(false), allocDim(0)
{
....
}
This is of course the start of a constructor for MatGSO. Which type does
this function return? class ZT or class FT?
Why do we need to pass the matrices of type ZT by reference (use of &)?
Isn't this the default way it is done for matrices?
What does the following code mean:

template<class ZT, class FT>
class MatGSO {
....
};

Is the class MatGSO a subclass of ZT or FT or of type ZT or FT, and if
yes, how do we distinguish the superclass/type of MatGSO?

Thank you again!
 
V

Victor Bazarov

To be more specific:

template<class ZT, class FT>
MatGSO<ZT, FT>::MatGSO(Matrix<ZT>& argB, Matrix<ZT>& argU, Matrix<ZT>& argUInvT,
int flags) :
b(argB),
enableIntGram(flags & GSO_INT_GRAM),
enableRowExpo(flags & GSO_ROW_EXPO),
enableTransform(argU.getRows() > 0),
enableInvTransform(argUInvT.getRows() > 0),
rowOpForceLong(flags & GSO_OP_FORCE_LONG),
u(argU), uInvT(argUInvT),
nKnownRows(0), nSourceRows(0), nKnownCols(0),
colsLocked(false), allocDim(0)
{
...
}
This is of course the start of a constructor for MatGSO. Which type does
this function return?

The constructor constructs an object. It has no return type.
class ZT or class FT?

Nope. It constructs an instance of MatGSO said:
Why do we need to pass the matrices of type ZT by reference (use of &)?

It's the usual way of avoiding making unnecessary copies.
Isn't this the default way it is done for matrices?
No.

What does the following code mean:

template<class ZT, class FT>
class MatGSO {
...
};

It's the [beginning of the] definition of the class template.
Is the class MatGSO a subclass of ZT or FT or of type ZT or FT, and if
yes, how do we distinguish the superclass/type of MatGSO?

There is no "superclass" here. MatGSO is a template with two arguments.

What book are you reading that doesn't explain what a template is?

V
 
S

Stuart

To be more specific:

template<class ZT, class FT>
MatGSO<ZT, FT>::MatGSO(Matrix<ZT>& argB, Matrix<ZT>& argU, Matrix<ZT>& argUInvT,
int flags) :
b(argB),
enableIntGram(flags & GSO_INT_GRAM),
enableRowExpo(flags & GSO_ROW_EXPO),
enableTransform(argU.getRows() > 0),
enableInvTransform(argUInvT.getRows() > 0),
rowOpForceLong(flags & GSO_OP_FORCE_LONG),
u(argU), uInvT(argUInvT),
nKnownRows(0), nSourceRows(0), nKnownCols(0),
colsLocked(false), allocDim(0)
{
...
}
This is of course the start of a constructor for MatGSO. Which type does
this function return? class ZT or class FT?
Why do we need to pass the matrices of type ZT by reference (use of &)?
Isn't this the default way it is done for matrices?
What does the following code mean:

template<class ZT, class FT>
class MatGSO {
...
};

Is the class MatGSO a subclass of ZT or FT or of type ZT or FT, and if
yes, how do we distinguish the superclass/type of MatGSO?

MatGSO is a class template, so MatGSO is neither a class nor a type. It
becomes a type if it is instantiated. So MatGSO<int, int> is a type,
MatGSO<double,std::vector<int> > is a type, but not MatGSO alone. Note
that the MatGSO class template does not inherit from something, so
MatGSO<int, int> will have no base class (unless someone provided a
template specialization for MatGSO<int, int>). Note further that class
template instantiations are treated as completely different classes. So
MatGSO<int, int> and MatGSO<double, std::vector<int> > do not inherit
from one another and cannot access protected members of one another.

If you have trouble understanding this, you can look at class templates
like this: Whenever the compiler instantiates a class template, it
creates an ordinary class using the template parameters as suffix to the
name: MatGSO<int, int> gives you the type MatGSO_int_int, which is
completely unrelated to MatGSO_double_std_vector_int_ (how the
instantiated class is processed internally in the compiler is not
specified, this should only serve as an analogy).

Regards,
Stuart
 
S

spyrosthalkidis

Thank you for the valuable help!
Unfortunately, chapter 14 of Deitel's book covers mainly the cases
where the "typename" keyword is used and not the cases where it is
ommited. The case of a template with two arguments is barely mentioned.

Spyros H.
 
J

Jorgen Grahn

.
I do not want someone to teach me C++! I want someone to explain
me the specific use of templates!

With all respect, if you don't understand that simple template, you
*do* need someone to teach you that part of C++.

It does take some practice before it feels natural. One trick: often
the template is in practice only used with one or a few different
argument types.

Think away the template aspect and imagine it says Foo<int> instead of
template<class T> Foo<T>. Foo<int> is just a normal type which
happens to have funny characters in its name.

Then think about Foo<std::string> for a moment. Would that compile? If
not, why? That gives you some perspective.

Another trick if you can call it that, is to ignore the template
meta-programming area for now. You don't need to understand that to
make good use of templates.

/Jorgen
 
8

88888 Dihedral

Jorgen Grahnæ–¼ 2013å¹´2月1日星期五UTC+8上åˆ6時35分37秒寫é“:
On Wed, 2013-01-30, (e-mail address removed) wrote:

...





With all respect, if you don't understand that simple template, you

*do* need someone to teach you that part of C++.



It does take some practice before it feels natural. One trick: often

the template is in practice only used with one or a few different

argument types.



Think away the template aspect and imagine it says Foo<int> instead of

template<class T> Foo<T>. Foo<int> is just a normal type which

happens to have funny characters in its name.



Then think about Foo<std::string> for a moment. Would that compile? If

not, why? That gives you some perspective.



Another trick if you can call it that, is to ignore the template

meta-programming area for now. You don't need to understand that to

make good use of templates.



/Jorgen



--

// Jorgen Grahn <grahn@ Oo o. . .

\X/ snipabacken.se> O o .

Well, even the complex numbers are not provided
in the C++ language standard.

Anyway a lot programmers just wrote or got thier
own extensions in jobs.
 
8

88888 Dihedral

David Harmonæ–¼ 2013å¹´2月2日星期六UTC+8下åˆ2時44分17秒寫é“:
On Fri, 1 Feb 2013 09:28:15 -0800 (PST) in comp.lang.c++, 88888






Yes, they are.

#include <complex>

I am vage about this. Complex is not a built-in type
but a template type in C++.
 
J

Jorgen Grahn

Jorgen Grahn??? 2013???2???1????????????UTC+8??????6???35???37????????????
Well, even the complex numbers are not provided
in the C++ language standard.

Anyway a lot programmers just wrote or got thier
own extensions in jobs.

I fail to see the connection between my posting and your response.
Is there one?

/Jorgen
 
8

88888 Dihedral

(e-mail address removed)æ–¼ 2013å¹´2月2日星期六UTC+8下åˆ5時40分32秒寫é“:
But it is defined by the C++ standard.

I'll express my oppinions about the built-in types
in a strong typed language.

When vars of int, float, double, maybe qdouble or
whatever were mixed in an expression, the type lifting rule of the result is clearly defined.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top