Re: curiously recurring template pattern and default constructor
On Thursday, 14 August 2014 15:50:06 UTC+3, gas...@this.is.invalid wrote:
Hello all,
we have here some templated class which augments base classes, e.g:
template <typename BaseType>
class Dlg : public BaseType
That is not CRTP. The inheritance is upside down here.
In CRTP the template is reusable base class:
template <typename DerivedType>
class Dlg
It lets Dlg to call things from DerivedType without virtual
functions in that Dlg. To achieve curious recurrence we derive
our custom classes from it like that:
class MyDlg : public Dlg<MyDlg>
...
Your (non-CTRP) pattern is difficult to swallow because it attempts
to extend anything. It is impossible on general case so you have to
partially specialize to limit down what you extend and how.
{
public:
Dlg(){}
template <typename Arg1>
Dlg(Arg1 arg1)
: BaseType(arg1)
{
}
template <typename Arg1, typename Arg2>
Dlg(Arg1 arg1, Arg2 arg2)
: BaseType(arg1, arg2)
{
}
//etc.
};
Member templates are only instantiated on request. However the default
constructor is always instantiated and this gives problems if the base
class lacks a default constructor.
Is there a standard solution for this problem?
I do not know of one, such a need too rarely arises. You can make a
metaprogram that checks if a type is default-constructible:
template<typename T>
class is_default_constructible
{
// make two different types
typedef char works;
struct doesnt_work {char a[2];};
// try to substitute default construction
template<typename U>
static decltype(U(), works()) test(int);
// fall here if failed to substitute
template<typename>
static doesnt_work test(...);
public:
static const bool value = sizeof(test<T>(0)) == sizeof(works);
};
Then you can use it to make partial specializations of whole class
depending if the base is default-constructible or not. However if
the goal is to achieve the effect of CRTP (no need for virtual
member functions) then it is lot easier to use actual CRTP.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]