Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Class and member templates, Default template argument, and Specialization

Reply
Thread Tools

Class and member templates, Default template argument, and Specialization

 
 
danilo.horta@gmail.com
Guest
Posts: n/a
 
      03-08-2006
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

 
Reply With Quote
 
 
 
 
amparikh@gmail.com
Guest
Posts: n/a
 
      03-08-2006

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

 
Reply With Quote
 
 
 
 
danilo.horta@gmail.com
Guest
Posts: n/a
 
      03-09-2006
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 ****, 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

 
Reply With Quote
 
danilo.horta@gmail.com
Guest
Posts: n/a
 
      03-11-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Full specialization of a member function template of a class template Dave C++ 4 06-04-2010 12:15 PM
template member specialization of template class toton C++ 2 02-22-2008 12:28 PM
Specialization of member function template in template class? Joseph Turian C++ 4 03-20-2006 10:07 AM
Out-of-class-template member template specialization syntax BigMan C++ 1 03-26-2005 02:23 PM
Member template function specialization in a template class Ruben Campos C++ 3 02-15-2005 10:40 AM



Advertisments