473,803 Members | 3,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2192

da**********@gm ail.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
5781
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
4
2589
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 R> class A { void foo(); }
9
1969
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
2274
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 ~A() ;
6
2767
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 no errors compiling this:
4
3338
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
2321
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 want to know how it works with the overloading of get<>(). boost::tupple<int,doubletup(1,2.0); double d=tup.get<2>(); // equal 2.0 // simple set up,
5
1871
by: edd | last post by:
Hello all, Please consider: template<typename Tclass my_class; template<class my_class<int> { //... };
8
2971
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
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10068
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9119
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.