Hello,
To start with I don't know if the way I am skinning this cat is a good way
or not, so if there is a better way of doing it all together please let me
know.
I want to enable functionality (members functions + variables) in a
template
class based on a compile time known flag such that if the flag is false
then
trying to use the functions causes a compile error. What I have tried is
deriving from a base class with a flag which works ok to include/exclude
variable and functionality from the optional bit. However, I can't figure
out how then to get access to the base class functions.
I failed miserably at passing the derived type as a template parameter
because I couldn't figure out how to then make the explicit <T, true>
version of the extra stuff class to work.
e.g.
template<bool enable> class CBit { };
template<> class CBit<true> { ... stuff... };
template<class T, bool incl> class CBoss : public CBit<incl> {...
stuff...};
This is fine but I don't know how to do it with...
template<class T, bool enable> class CBit { };
template<urm?...
(is this actually a partial specialisation because I read on the internet
that you can't keep a type from the original declaration in the partial
specialisation?)
Is there a way of getting the type sent through as a template argument so
I
can use it?
Is there a way of determining the type of your derived class regardless of
anything else? (The types are all static so you can check an assumed type
with satic_cast but can you find it somehow & make a typedef out of it?)
Source of a test program is below.
Thanks,
Chris
-------------------------------------
#include <stdio.h>
#include <conio.h>
template<bool enable>
class CBit { };
template<>
class CBit<true>
{
public:
CBit() {k=99;}
void stuff();
private:
int get() {return k;}
int k;
};
template<class T, bool incl>
class CBoss
: private CBit<incl>
{
typedef CBoss<T,incl> myT;
friend typename CBit<incl>;
public:
void foo()
{
printf("foo()\n");
stuff();
printf("\n\n");
}
private:
void bar(int g)
{
printf("bar() = %d\n", g);
}
int bossstuff;
};
int main(int argc, char *argv[ ])
{
CBoss<int,false> my;
CBoss<int,true> myTrue;
printf("sizeof: my=%d, myTrue=%d\n",sizeof(my),sizeof(myTrue));
// my.foo();
myTrue.foo();
_getch();
return 0;
}
template<> void CBit<true>::stuff()
{
printf("stuff, true\n");
typedef CBoss<int,true> D; // How to get this type at compile time???
//Obviously this only works because I know I have a <int,true> in the
sample!
D* pDerived;
pDerived = static_cast<D*>(this);
pDerived->bar( get() );
}
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|