473,466 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Partial Specialization Method

I have a matrix class and I would like to add a method that is only
applicable when the template type is a of another class. How can I
specialize the class to allow for the new method and hide it with any
other type?

template<class Tclass Matrix
{
public:
Matrix();
Matrix(Ini
&ini); // Only
applicable with MyClass
Matrix(unsigned w, unsigned h, T &d = T());

void ImportIni(Ini
&ini); // Only applicable
with MyClass
void ExportIni(std::ofstream &fout, const String &val);// Only
applicable with MyClass

void AddRow(T &d = T());
void AddCol(T &d = T());
bool InsertRow(unsigned pos, T &d = T());
bool InsertCol(unsigned pos, T &d = T());
bool DeleteRow(unsigned pos);
bool DeleteCol(unsigned pos);

bool Set(unsigned row, unsigned col, T &s);

T Get(unsigned row, unsigned col);
std::vector<TGetRow(unsigned row);
std::vector<TGetCol(unsigned col);
unsigned GetHeight();
unsigned GetWidth();

T operator[](Point &p);
std::vector<Toperator[](int col);
private:
std::vector< std::vector<T matrix;
unsigned height;
unsigned width;
};

May 6 '07 #1
4 1834
On Sun, 06 May 2007 09:11:10 -0700, MathStuf wrote:
I have a matrix class and I would like to add a method that is only
applicable when the template type is a of another class. How can I
specialize the class to allow for the new method and hide it with any
other type?

template<class Tclass Matrix
{
public:
Matrix();
Matrix(Ini
&ini); // Only
applicable with MyClass
Matrix(unsigned w, unsigned h, T &d = T());
It is illegal to bind a rvalue to a non-const reference. Make this a
const T & if possible.
void ImportIni(Ini
&ini); // Only applicable
with MyClass
void ExportIni(std::ofstream &fout, const String &val);// Only
applicable with MyClass

void AddRow(T &d = T());
void AddCol(T &d = T());
bool InsertRow(unsigned pos, T &d = T()); bool InsertCol(unsigned
pos, T &d = T()); bool DeleteRow(unsigned pos);
bool DeleteCol(unsigned pos);

bool Set(unsigned row, unsigned col, T &s);

T Get(unsigned row, unsigned col);
std::vector<TGetRow(unsigned row);
std::vector<TGetCol(unsigned col);
unsigned GetHeight();
unsigned GetWidth();

T operator[](Point &p);
std::vector<Toperator[](int col);
private:
std::vector< std::vector<T matrix; unsigned height;
unsigned width;
};
You can do it with template specialization, like so:

template<class Tstruct Base
{
// all common stuff from above
// ...
};

template<class Tstruct Matrix : Base<T>
{
Matrix();
Matrix(unsigned w, unsigned h);
};

class MyClass;
class Ini;

template<struct Matrix<MyClass: Base<MyClass>
{
Matrix();
Matrix(unsigned w, unsigned h);

// additional stuff
Matrix(Ini &ini);
// ...
};
There is some unavoidable duplication for the constructors unfortunately.

--
Markus
May 6 '07 #2
On May 6, 12:56 pm, Markus Schoder <a3vr6dsg-use...@yahoo.dewrote:
On Sun, 06 May 2007 09:11:10 -0700, MathStuf wrote:
I have a matrix class and I would like to add a method that is only
applicable when the template type is a of another class. How can I
specialize the class to allow for the new method and hide it with any
other type?
template<class Tclass Matrix
{
public:
Matrix();
Matrix(Ini
&ini); // Only
applicable with MyClass
Matrix(unsigned w, unsigned h, T &d = T());

It is illegal to bind a rvalue to a non-const reference. Make this a
const T & if possible.
void ImportIni(Ini
&ini); // Only applicable
with MyClass
void ExportIni(std::ofstream &fout, const String &val);// Only
applicable with MyClass
void AddRow(T &d = T());
void AddCol(T &d = T());
bool InsertRow(unsigned pos, T &d = T()); bool InsertCol(unsigned
pos, T &d = T()); bool DeleteRow(unsigned pos);
bool DeleteCol(unsigned pos);
bool Set(unsigned row, unsigned col, T &s);
T Get(unsigned row, unsigned col);
std::vector<TGetRow(unsigned row);
std::vector<TGetCol(unsigned col);
unsigned GetHeight();
unsigned GetWidth();
T operator[](Point &p);
std::vector<Toperator[](int col);
private:
std::vector< std::vector<T matrix; unsigned height;
unsigned width;
};

You can do it with template specialization, like so:

template<class Tstruct Base
{
// all common stuff from above
// ...

};

template<class Tstruct Matrix : Base<T>
{
Matrix();
Matrix(unsigned w, unsigned h);

};

class MyClass;
class Ini;

template<struct Matrix<MyClass: Base<MyClass>
{
Matrix();
Matrix(unsigned w, unsigned h);

// additional stuff
Matrix(Ini &ini);
// ...

};

There is some unavoidable duplication for the constructors unfortunately.

--
Markus
Would it just be easier to make methods that throw() for the other
types then?

--MathStuf

May 6 '07 #3
On Sun, 06 May 2007 10:01:57 -0700, MathStuf wrote:
On May 6, 12:56 pm, Markus Schoder <a3vr6dsg-use...@yahoo.dewrote:
>On Sun, 06 May 2007 09:11:10 -0700, MathStuf wrote:
I have a matrix class and I would like to add a method that is only
applicable when the template type is a of another class. How can I
specialize the class to allow for the new method and hide it with any
other type?
template<class Tclass Matrix
{
public:
Matrix();
Matrix(Ini
&ini); // Only
applicable with MyClass
Matrix(unsigned w, unsigned h, T &d = T());

It is illegal to bind a rvalue to a non-const reference. Make this a
const T & if possible.
void ImportIni(Ini
&ini); // Only applicable
with MyClass
void ExportIni(std::ofstream &fout, const String &val);// Only
applicable with MyClass
void AddRow(T &d = T());
void AddCol(T &d = T());
bool InsertRow(unsigned pos, T &d = T()); bool
InsertCol(unsigned pos, T &d = T()); bool DeleteRow(unsigned
pos); bool DeleteCol(unsigned pos);
bool Set(unsigned row, unsigned col, T &s);
T Get(unsigned row, unsigned col);
std::vector<TGetRow(unsigned row);
std::vector<TGetCol(unsigned col);
unsigned GetHeight();
unsigned GetWidth();
T operator[](Point &p);
std::vector<Toperator[](int col);
private:
std::vector< std::vector<T matrix; unsigned height; unsigned
width;
};

You can do it with template specialization, like so:

template<class Tstruct Base
{
// all common stuff from above
// ...

};

template<class Tstruct Matrix : Base<T{
Matrix();
Matrix(unsigned w, unsigned h);

};

class MyClass;
class Ini;

template<struct Matrix<MyClass: Base<MyClass{
Matrix();
Matrix(unsigned w, unsigned h);

// additional stuff
Matrix(Ini &ini);
// ...

};

There is some unavoidable duplication for the constructors
unfortunately.

--
Markus

Would it just be easier to make methods that throw() for the other types
then?
Yes probably, but I would rather not implement these methods for the
other types at all or even better use a compile time assertion. That way
the linker resp. compiler will tell you if someone tries to use these
methods with the wrong class.

--
Markus
May 6 '07 #4

"MathStuf" <Ma******@gmail.comwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
>I have a matrix class and I would like to add a method that is only
applicable when the template type is a of another class. How can I
specialize the class to allow for the new method and hide it with any
other type?

template<class Tclass Matrix
{
public:
Matrix();
Matrix(Ini
&ini); // Only
applicable with MyClass
Matrix(unsigned w, unsigned h, T &d = T());

void ImportIni(Ini
&ini); // Only applicable
with MyClass
void ExportIni(std::ofstream &fout, const String &val);// Only
applicable with MyClass

void AddRow(T &d = T());
void AddCol(T &d = T());
bool InsertRow(unsigned pos, T &d = T());
bool InsertCol(unsigned pos, T &d = T());
bool DeleteRow(unsigned pos);
bool DeleteCol(unsigned pos);

bool Set(unsigned row, unsigned col, T &s);

T Get(unsigned row, unsigned col);
std::vector<TGetRow(unsigned row);
std::vector<TGetCol(unsigned col);
unsigned GetHeight();
unsigned GetWidth();

T operator[](Point &p);
std::vector<Toperator[](int col);
private:
std::vector< std::vector<T matrix;
unsigned height;
unsigned width;
};
You can use SFINAE

template<class Tclass Matrix
{
public:
/* ... */
template<class U>
Matrix(U yourIntParam,
typename enable_if_c<
is_same<T,MyClass>::value &&
is_convertible<U,int>::value
, int>::type = 0);
};

Now that constructor will only be considered by the compiler if T is MyClass
(and if the passed U is convertible to int, which, alas, is the downside for
making the constructor itself a template, as that is needed for SFINAE to be
able to work). Definitions of enable_if_c<>, is_same<and is_convertible<>
can be found in boost, but I'll provide them here for completeness' sake.

template<bool B, class Tstruct enable_if_c { };
template<class Tstruct enable_if_c<true, T{ typedef T type; };

template<class T, class Ustruct is_same { static const bool value =
false; };
template<class Tstruct is_same<T,T{ static const bool value = true; };

template<class T, class Ustruct is_convertible
{
private:
struct small { char c; };
struct large { char c[2]; };
static small foo(const U & u);
static large foo(...);
public:
static const bool value = (sizeof(foo(*(T*)1)) == sizeof(small));
};

- Sylvester Hesp
May 7 '07 #5

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

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
9
by: Philip Lawatsch | last post by:
Hi I'd like to implement some kind if type traits myself, but I have to support broken compilers (like visual studio) that do not support Partial Specialization. My first shot was something...
8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
1
by: BekTek | last post by:
I'm still confused about the template partial specialization which is used in many libraries.. due to lack of introduction for beginner.. Could you tell me about that in short? Thanks in...
5
by: __PPS__ | last post by:
Hello, I want to write specialized method for a class: template<class A, class B> class xxx{ A a; B b; operator bool()const{ a==b; } };
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
4
by: wakun | last post by:
Hi there, I am learning template programming. When testing the partial specialization, I have some probelms Here is a full templated class template <typename T, int n> class CT { public: T...
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...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
9
by: Greg | last post by:
Hi, I would like to specify behavior of a class member relatively to template implemetation. It works in usual cases but it seems to fail with to templates when one of the two is specified... ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.