|
Boost Users : |
From: Steven van Dijk (SDijk_at_[hidden])
Date: 2006-05-22 10:29:16
Hi,
I want to prevent non-factory creation of my classes. All classes have
constructors without arguments. The constructor of a class should be
'protected' and a special creator template class is used to derive from
the class and give the factory access to it. Example:
class A : public Base
{
protected:
A();
public:
void DoSomething();
};
template < class T >
class Creator : public T
{
public:
static Base* Create()
{
return new Creator<T>;
}
};
...
class Factory
{
static A* GetAObject()
{
return Creator<A>::Create();
}
};
The example is somewhat contrived to omit non-essential details.
Anyway, what I would like to do is get a compiler error when I register
a class with the factory that has a publicly accessible constructor. I
tried to do this with boost::has_trivial_constructor< T > like this:
class A : public Base
{
public:
A() {}
};
template < bool B >
class CGiveError
{
public:
static void error() {}
};
template <>
class CGiveError<true>
{
public:
static void error() { assert( false ); }
};
void
RegisterAll()
{
CGiveError< boost::has_trivial_constructor< A >::value
>::error();
}
However, this doesn't work. I guess has_trivial_constructor just checks
if the constructor takes arguments or not.
Is there some way to achieve what I want? That is, issue an error if the
constructor is public?
Thanks,
Steven
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net