Class and member templates, Default template argument, and Specialization

D

danilo.horta

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
 
A

amparikh

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;
}
 
D

danilo.horta

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
 
D

danilo.horta

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.
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top