Template Question

Discussion in 'C++' started by spyrosthalkidis@gmail.com, Jan 30, 2013.

  1. Guest

    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.
     
    , Jan 30, 2013
    #1
    1. Advertising

  2. Öö Tiib Guest

    On Wednesday, 30 January 2013 07:19:05 UTC+2, 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".
     
    Öö Tiib, Jan 30, 2013
    #2
    1. Advertising

  3. Guest

    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.
     
    , Jan 30, 2013
    #3
  4. Guest

    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!
     
    , Jan 30, 2013
    #4
  5. On 1/30/2013 9:18 AM, 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
     
    Victor Bazarov, Jan 30, 2013
    #5
  6. Stuart Guest

    On 01/30/2013 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
     
    Stuart, Jan 30, 2013
    #6
  7. Guest

    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.
     
    , Jan 30, 2013
    #7
  8. Jorgen Grahn Guest

    On Wed, 2013-01-30, 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 .
     
    Jorgen Grahn, Jan 31, 2013
    #8
  9. Jorgen Grahnæ–¼ 2013å¹´2月1日星期五UTC+8上åˆ6時35分37秒寫é“:
    > On Wed, 2013-01-30, 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, Feb 1, 2013
    #9
  10. 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 <> 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++.
     
    88888 Dihedral, Feb 2, 2013
    #10
  11. Jorgen Grahn Guest

    On Fri, 2013-02-01, 88888 Dihedral wrote:
    > Jorgen Grahn??? 2013???2???1????????????UTC+8??????6???35???37????????????
    >> On Wed, 2013-01-30, 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.


    > 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

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Feb 2, 2013
    #11
  12. æ–¼ 2013å¹´2月2日星期六UTC+8下åˆ5時40分32秒寫é“:
    > On Sat, 2 Feb 2013 00:39:40 -0800 (PST), 88888 Dihedral
    >
    > <> wrote:
    >
    >
    >
    > >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 <> 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++.

    >
    >
    >
    >
    >
    > 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.
     
    88888 Dihedral, Feb 2, 2013
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    502
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    567
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,147
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    806
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    559
Loading...

Share This Page