template and variable parameters

Discussion in 'C++' started by martin.druon@wanadoo.fr, Dec 13, 2005.

  1. Guest

    Hi,

    I created a template class to represent hypermatrix. I would like to
    add methods where the number of parameters are checked during the
    compilation time. For example :

    template <size_t dim>
    class Matrix
    {
    protected :
    ... // member datas

    public:
    ...
    Matrix( ????? ) { } // -> all of my parameters are "int"
    ...

    void create ( ????? ) { } // -> all of my parameters are "int"
    }

    template <>
    class Matrix<0>
    {
    protected :
    ... // member datas

    public:
    ...
    Matrix( ????? ) { }
    ...

    void create ( ????? ) { }
    }



    in main.cpp :


    int main(int argc, char **argv)
    {
    Matrix<3> m1(2, 5, 4); // OK
    Matrix<3> m2(2, 5, 4, 8, 3); // compilation error

    Matrix<3> m3; // OK
    m3.create(4, 6, 5); // OK
    m3.create(4); // compilation error
    }

    How I can do that ???

    I try recursive method (with "operator ," overloading, ...) and with
    lists but :
    - either the number of parameters is not checked
    - either the syntax is not like I want (without {...} or int[] = ...)

    I try also to overload the "cast operator" but I never success... for
    example to do this :

    before : 1,2; // -> the ",2" is ignored (not "operator ," defined
    with "int")
    after : 1,2; // "1" is automaticaly cast to a new created class
    (Lst for example) so the "," of ",2" is the "operator ," of this class
    Lst

    Do you understand ???

    All of your ideas are welcome...

    Thanks
     
    , Dec 13, 2005
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > Hi,
    >
    > I created a template class to represent hypermatrix. I would like to
    > add methods where the number of parameters are checked during the
    > compilation time. For example :
    >
    > template <size_t dim>
    > class Matrix
    > {
    > protected :
    > ... // member datas
    >
    > public:
    > ...
    > Matrix( ????? ) { } // -> all of my parameters are "int"
    > ...
    >
    > void create ( ????? ) { } // -> all of my parameters are "int"
    > }
    >
    > template <>
    > class Matrix<0>
    > {
    > protected :
    > ... // member datas
    >
    > public:
    > ...
    > Matrix( ????? ) { }
    > ...
    >
    > void create ( ????? ) { }
    > }
    >
    >
    >
    > in main.cpp :
    >
    >
    > int main(int argc, char **argv)
    > {
    > Matrix<3> m1(2, 5, 4); // OK
    > Matrix<3> m2(2, 5, 4, 8, 3); // compilation error
    >
    > Matrix<3> m3; // OK
    > m3.create(4, 6, 5); // OK
    > m3.create(4); // compilation error
    > }
    >
    > How I can do that ???
    >
    > I try recursive method (with "operator ," overloading, ...) and with
    > lists but :
    > - either the number of parameters is not checked
    > - either the syntax is not like I want (without {...} or int[] = ...)
    >
    > I try also to overload the "cast operator" but I never success... for
    > example to do this :
    >
    > before : 1,2; // -> the ",2" is ignored (not "operator ," defined
    > with "int")
    > after : 1,2; // "1" is automaticaly cast to a new created class
    > (Lst for example) so the "," of ",2" is the "operator ," of this class
    > Lst
    >
    > Do you understand ???
    >
    > All of your ideas are welcome...
    >
    > Thanks


    There are two solutions that I see:

    1. Use partial specialization:

    template<unsigned dims>
    struct Matrix
    {
    Matrix( unsigned, unsigned );
    Matrix( unsigned, unsigned, unsigned );
    // ...
    };

    template<>
    class Matrix<2>::Matrix(
    unsigned height,
    unsigned width )
    { /*...*/ }

    template<>
    class Matrix<3>::Matrix(
    unsigned height,
    unsigned width,
    unsigned depth )
    { /*...*/ }

    Note that you should not define Matrix(unsigned,unsigned,unsigned) for
    Matrix<2> or Matrix(unsigned,unsigned) for Matrix<3>. Then any attempt
    to link will fail if those functions are called.

    2. You could use method chaining (cf.
    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.18) with a
    CreateMatrix object that has SetHeight(), SetWidth(), and SetDepth()
    members or a generic SetDim() member.

    Cheers! --M
     
    mlimber, Dec 13, 2005
    #2
    1. Advertising

  3. chowy Guest

    Thanks...

    but with these methods, I can't use n-dimensional matrix...

    for example, to use a 5-d matrix, I must define before :

    Matrix( unsigned, unsigned, unsigned, unsigned, unsigned );

    template<>
    class Matrix<5>::Matrix(
    unsigned dim1,
    unsigned dim2,
    unsigned dim3,
    unsigned dim4,
    unsigned dim5 )
    { /*...*/ }

    and for a 8-d matrix... !!!!

    Perhaps it's impossible to do what I want ???
    Perhaps I must use a pre-processor before (to generate code) ???
     
    chowy, Dec 22, 2005
    #3
  4. Guest

    chowy wrote:
    > Thanks...
    >
    > but with these methods, I can't use n-dimensional matrix...
    >
    > for example, to use a 5-d matrix, I must define before :
    >
    > Matrix( unsigned, unsigned, unsigned, unsigned, unsigned );
    >
    > template<>
    > class Matrix<5>::Matrix(
    > unsigned dim1,
    > unsigned dim2,
    > unsigned dim3,
    > unsigned dim4,
    > unsigned dim5 )
    > { /*...*/ }
    >
    > and for a 8-d matrix... !!!!
    >
    > Perhaps it's impossible to do what I want ???
    > Perhaps I must use a pre-processor before (to generate code) ???


    If you do decide to go the preprocessor route, the boost preprocessor
    library might be useful to you.

    http://www.boost.org/libs/preprocessor/doc/index.html

    I've not used it so I don't know whether it would do what you need. But
    some of the examples in the documentation seem similar to your
    question.

    Gavin Deane
     
    , Dec 22, 2005
    #4
    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. ian
    Replies:
    5
    Views:
    541
  2. case2005
    Replies:
    3
    Views:
    1,813
    Nicolas Pavlidis
    Feb 13, 2005
  3. Froefel
    Replies:
    1
    Views:
    793
    Mark Rae [MVP]
    Jul 4, 2007
  4. kito
    Replies:
    2
    Views:
    420
  5. avasilev
    Replies:
    2
    Views:
    516
    avasilev
    Oct 6, 2011
Loading...

Share This Page