Topic: Adding to specialized templates
Author: gp.kiwi@gmail.com (Graeme Prentice)
Date: Fri, 29 Apr 2005 03:55:33 GMT Raw View
On Sun, 24 Apr 2005 20:25:01 CST, Samee Zahur wrote:
>I don't know if something like this is already there, but I find
>this pretty frustrating:
>
>template <class T> class cls
>{
> T var1,var2;
>public:
> mem1();
> mem2();
> //...
>};
>
>Now if want that cls<int> would have some member function in
>addition to whatever it has for other tempate params, do I
>name each and every member in the specialization as well?
>I know there is a way around this using inheritence, but
>ctors and assignment ops are never inherited, meaning I have
>to change the specializations AND derivations every time.
>I say there should be some way of making slight modifications
>to the template in a specialization without rewriting it
>from scratch.
There is already a way to do most of this. Data members that are not
common to all classes can be inherited from a base class. Member
functions can be added to the original class and made available to only
specific classes without costing other classes - you can use explicit
specialization for this as below, and you can actually partially
specialize member functions as well (a feature not provided directly by
C++), using a little bit of hackery. If you want some references to
this, here's one
http://makeashorterlink.com/?E3D6526FA
that shows two ways, one using SFINAE (which you could change to use
boost enable_if). If you want a compile time error (instead of a link
time error) for calls to a function that doesn't belong to that class
you can add some "type checking" code. See also this thread
http://makeashorterlink.com/?U2EE16AFA
You can also add data members to the class that are customised according
to the class type. You don't get a zero-sized member for an empty class
data member but an empty base class can have zero size (depends on the
compiler and compiler switches). You can also partially and explicitly
specialize member template classes.
class nothing{};
template <typename T, typename customising_base = nothing>
class cls;
template <typename>
struct cls_customise_types
{
typedef int type1;
typedef char type2;
};
template <>
struct cls_customise_types<cls<int> >
{
typedef long type1;
typedef short type2;
};
template <typename T,
typename customising_base /*= has default already*/ >
class cls : public customising_base
{
static const T v1 = 33;
T var1,var2;
typename cls_customise_types<cls >::type1 var3;
public:
void mem1();
void mem2();
//...
void cls_int_only_function();
};
template <>
void cls<int>::cls_int_only_function(){}
int main()
{
cls<int> a1;
a1.cls_int_only_function();
}
Graeme
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Samee Zahur" <samee.zahur@gmail.com>
Date: Sun, 24 Apr 2005 20:25:01 CST Raw View
I don't know if something like this is already there, but I find
this pretty frustrating:
template <class T> class cls
{
T var1,var2;
public:
mem1();
mem2();
//...
};
Now if want that cls<int> would have some member function in
addition to whatever it has for other tempate params, do I
name each and every member in the specialization as well?
I know there is a way around this using inheritence, but
ctors and assignment ops are never inherited, meaning I have
to change the specializations AND derivations every time.
I say there should be some way of making slight modifications
to the template in a specialization without rewriting it
from scratch.
Samee
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]