Boost logo

Boost :

Subject: [boost] a (useful?) crtp trick
From: pavel (paul.cpprules_at_[hidden])
Date: 2012-08-24 13:50:45

hi all
i post this in the hope someone finds this technique useful

to make long story short, here is a snippet:

   template<typename t> //tag deducing util
   struct base_tag
     typedef decltype(get_tag((t*)42)) type;
   template<typename type,
            typename tag = typename base_tag<type>::type>
   struct Base //crtp base class
     friend tag get_tag(type*); //helper declaration, never defined
   template<typename type> //no mention of tag
   void f(const Base<type>&) //tag is deduced automatically

   struct Foo : Base<Foo, void> //derived type
   int main()
     Foo foo;

the idea is that if we have an associated type (or types) along with
the "real" type in the crtp pattern we can make that associated type
deduced automatically

and even pretend there is no such tag leaving it e.g. for advanced usage

more on this here:

if you notice a grammar mistake or weird phrasing in my message
please point it out

Boost list run by bdawes at, gregod at, cpdaniel at, john at