Yes, this is possible through partial specialization. Is this another question recommended? To begin with, this solution does not scale, since you need 2 ^ n specializations, where n is the number of variables that you control access to. And do you really want to change the interface of your class based on the value of the template parameter?
It seems that you are creating something difficult to maintain, which is hard to understand and overly smart.
However, if you decide that this is a good idea, here is how you do it:
template <typename T, bool publicBar=true> class Foo { public: T bar; }; template <typename T> class Foo<T,false> { protected: T bar; };
Joe gauterin
source share