Class and member templates, Default template argument, and Specialization

Discussion in 'C++' started by danilo.horta@gmail.com, Mar 8, 2006.

  1. Guest

    Hi guys

    I'm trying to accomplish a slightly difficult task. I think it's more
    easy to explain trought an unworking code:

    template<class T, size_t numDim> VecBasis {/*...*/};
    typedef VecBasis<float, 3> vec3;

    template<class T, size_t size> class MatBasis {
    //...
    template<class V = VecBasis<T, size>> V column(size_t c) const {
    //...
    }
    };

    template <class T>
    template<>
    inline V MatBasis<T, 3>::column<vec3>(size_t c)
    {
    //...
    }

    From the member defined inside the class, I want VecBasis<T, size> as
    default template argument. In other words, a type generated by
    VecBasis<> template that depends on the MatBasis<>'s template
    arguments.
    From the other, the user specialized one, I want the vec3 type as
    member's template argument when, as you can see, the MatBasis<> uses
    size = 3.

    How could I accomplish it?

    Cheers
    , Mar 8, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > Hi guys
    >
    > I'm trying to accomplish a slightly difficult task. I think it's more
    > easy to explain trought an unworking code:
    >
    > template<class T, size_t numDim> VecBasis {/*...*/};
    > typedef VecBasis<float, 3> vec3;
    >
    > template<class T, size_t size> class MatBasis {
    > //...
    > template<class V = VecBasis<T, size>> V column(size_t c) const {
    > //...
    > }
    > };
    >
    > template <class T>
    > template<>
    > inline V MatBasis<T, 3>::column<vec3>(size_t c)
    > {
    > //...
    > }
    >
    > From the member defined inside the class, I want VecBasis<T, size> as
    > default template argument. In other words, a type generated by
    > VecBasis<> template that depends on the MatBasis<>'s template
    > arguments.
    > From the other, the user specialized one, I want the vec3 type as
    > member's template argument when, as you can see, the MatBasis<> uses
    > size = 3.
    >
    > How could I accomplish it?
    >
    > Cheers


    Not 100% sure what you are trying to do but here is what one possible
    solution would look like based on the information you have provided.

    template<class T, size_t numDim>
    class VecBasis
    {
    };

    typedef VecBasis<float, 3> vec3;

    template<class T, size_t size>
    class MatBasis
    {
    template<class V >
    V column(size_t c) const;
    };

    template<class T>
    class MatBasis<T, 3>
    {
    public:
    template<class V >
    V column() const;
    };

    template<>
    class MatBasis<float, 3>
    {
    public:
    template<class V >
    V column() const;
    };


    template<>
    inline vec3 MatBasis<float, 3>::column<vec3>() const
    {
    static vec3 v;
    return v;
    }
    , Mar 8, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > Not 100% sure what you are trying to do but here is what one possible
    > solution would look like based on the information you have provided.
    >
    > template<class T, size_t numDim>
    > class VecBasis
    > {
    > };
    >
    > typedef VecBasis<float, 3> vec3;
    >
    > template<class T, size_t size>
    > class MatBasis
    > {
    > template<class V >
    > V column(size_t c) const;
    > };
    >
    > template<class T>
    > class MatBasis<T, 3>
    > {
    > public:
    > template<class V >
    > V column() const;
    > };
    >
    > template<>
    > class MatBasis<float, 3>
    > {
    > public:
    > template<class V >
    > V column() const;
    > };
    >
    >
    > template<>
    > inline vec3 MatBasis<float, 3>::column<vec3>() const
    > {
    > static vec3 v;
    > return v;
    > }


    Oh shit, I made a mistake when I tried to simply my code. In reality,
    I'm not using "typedef VecBasis<float, 3> vec3;". __vec3 is another
    class__.

    Sorry guys :/. change "typedef VecBasis<float, 3> vec3;" by "class
    vec3 {};".

    Amparikh, your solution uses a class template specialization for
    which I'm forced to do inheritance because the specialized class is
    exactly the same as the non-specialized one except to the column
    member. It doesn't seem to be a nice approach.

    What I want to do is make a member (the column) that uses VecBasis<>
    as its template parameter by default and, when its class's second
    template parameter is 3, that member uses the vec3 type as its template
    parameter. As follows:

    MatBasis<float, 2> mat0;
    mat0.column(0); // returns a VecBasis<float, 2>

    MatBasis<float, 3> mat1;
    mat1.column(0); // returns a vec3

    Thanks
    , Mar 9, 2006
    #3
  4. Guest

    No answer? Am I not being very clearly? What I'm trying to do seems
    to be logically tangible... I just don't know how to express it in C++
    code.
    , Mar 11, 2006
    #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. Dave
    Replies:
    4
    Views:
    7,651
    pdixtl
    Jun 4, 2010
  2. Ruben Campos
    Replies:
    3
    Views:
    6,452
  3. BigMan
    Replies:
    1
    Views:
    521
  4. Joseph Turian
    Replies:
    4
    Views:
    571
    John Carson
    Mar 20, 2006
  5. toton
    Replies:
    2
    Views:
    323
    toton
    Feb 22, 2008
Loading...

Share This Page