473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Class and member templates, Default template argument, and Specialization

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 '06 #1
3 2156

da**********@gmail.com 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 '06 #2
am******@gmail.com 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 '06 #3
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 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
4
by: SainTiss | last post by:
Hi, From what I've read in several places, it seems that explicit specialization of member functions of class templates is allowed, but partial specialization isn't: template<class T, class...
9
by: Ian | last post by:
Can it be done? If so, what's the syntax. For example a full specialisation, template <typename T> struct X { template <typename C> void foo( C a ) {} };
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
4
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
5
by: edd | last post by:
Hello all, Please consider: template<typename Tclass my_class; template<class my_class<int> { //... };
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.