Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Template Question (http://www.velocityreviews.com/forums/t957076-template-question.html)

spyrosthalkidis@gmail.com 01-30-2013 05:19 AM

Template Question
 
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 01-30-2013 08:57 AM

Re: Template Question
 
On Wednesday, 30 January 2013 07:19:05 UTC+2, spyrost...@gmail.com wrote:
> 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".


spyrosthalkidis@gmail.com 01-30-2013 10:53 AM

Re: Template Question
 
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.

spyrosthalkidis@gmail.com 01-30-2013 02:18 PM

Re: Template Question
 
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!

Victor Bazarov 01-30-2013 02:38 PM

Re: Template Question
 
On 1/30/2013 9:18 AM, spyrosthalkidis@gmail.com wrote:
> 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<ZT,FT>.

> 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
--
I do not respond to top-posted replies, please don't ask

Stuart 01-30-2013 02:49 PM

Re: Template Question
 
On 01/30/2013 spyrosthalkidis@gmail.com wrote:
> 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


spyrosthalkidis@gmail.com 01-30-2013 03:41 PM

Re: Template Question
 
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.

Jorgen Grahn 01-31-2013 10:35 PM

Re: Template Question
 
On Wed, 2013-01-30, spyrosthalkidis@gmail.com wrote:
....
> 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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

88888 Dihedral 02-01-2013 05:28 PM

Re: Template Question
 
Jorgen Grahn於 2013年2月1日星期五UTC+8上午6時35分37秒 道:
> On Wed, 2013-01-30, spyrosthalkidis@gmail.com wrote:
>
> ...
>
> > 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
>
>
>
> --
>
> // 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.

88888 Dihedral 02-02-2013 08:39 AM

Re: Template Question
 
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
>
> Dihedral <dihedral88888@googlemail.com> wrote,
>
> >Well, even the complex numbers are not provided

>
> >in the C++ language standard.

>
>
>
> Yes, they are.
>
> #include <complex>


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




All times are GMT. The time now is 06:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.