$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [boost] a (useful?) crtp trick
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2012-08-25 11:31:53
On 24/08/2012 19:50, pavel wrote:
> 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;
>       f(foo);
>     }
What's the advantage over
template<class T>
struct tag_of;
template<class Derived>
struct Base
{
   typedef typename tag_of<Derived>::type tag;
};
struct Foo : Base<Foo>
{
};
template<>
struct tag_of<Foo>
{
   typedef void type;
};