Re: Using a STL container of type self in a class declaration
Richard Smith wrote:
On Nov 8, 12:36 am, annamalai <annamalai.gurus...@gmail.com> wrote:
Does this mean that the CRTP for standard library containers are
undefined? For example,
struct B : std::list<B>
{
};
Yes, it is undefined. But why would you want to do this?
The
Curiously Recurring Template Pattern isn't something you can do with
an arbitrary template: it requires a template that was designed to
support it. Generally, it's because the base class does something
like:
template <class Derived>
struct base {
void foo() { static_cast<Derived*>(this)->bar(); }
};
The std::list template does not do anything like that.
Just to give one possible reason as to _why_ someone might want to do
something like that: I have a class that is defined just as
struct pure_finite_set
: public std::set< completion< pure_finite_set > > {
}; // pure_finite_set
// freestanding IO and operators for set operations omitted
which would be done without the completion<>, if I had a set<> template that
did not induce undefined behavior when the type parameter is incomplete.
The class models finite sets without atoms. Typical elements are
zero = 0
one = { 0 }
two = { 0, { 0 } }
three = { 0, { 0 }, { 0, { 0 } } }
four = { 0, { 0 }, { 0, { 0 } }, { 0, { 0 }, { 0, { 0 } } } }
even = { 0, { 0, { 0 } } }
odd = { { 0 }, { 0, { 0 }, { 0, { 0 } } } }
two + even = { 0, { 0 }, { 0, { 0 } } }
even * three = { 0, { 0, { 0 } } }
odd ^ even = { 0, { 0 }, { 0, { 0 } }, { 0, { 0 }, { 0, { 0 } } } }
three - two = { { 0, { 0 } } }
where 0 denotes the empty set {}.
Best
Kai-Uwe Bux
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]