News for hackers. Stuff that matters.
Re:Template Metaprogramming with Modern C++: templates in depth by cbsmith on Monday September 29, 2014 @04:34PM (#8385126)
In truth, if you really care about it, you can take a policy based design approach to this and have explicit rules for when you may or may not do type erasure. It's definitely a bit of work, but you can actually get full compile_time type safety and yet avoid having duplicative code with various tricks like (this skips over a lot of the pain and isn't really how you'd do it... just illustrative):

    template <std::size_t N>
    struct raw {
        template <typename U>
        constexpr raw(U x, std::enable_if<.....>);

        template <typename U>
        constexpr operator U();
    };

    template <template ContainerType, typename T, std::enable_if<std::is_trivially_convertible<T, ....> > >
    struct somewhat_erased_container :  private ContainerType<raw<sizeof(T)>> {
    };
It's particularly easy to do with containers of pointers (of course).
Re:Template Metaprogramming with Modern C++: templates in depth by dllthomas on Monday September 29, 2014 @04:39PM (#8385159)
Very nice.
Re:Template Metaprogramming with Modern C++: templates in depth by cbsmith on Monday September 29, 2014 @05:45PM (#8385499)
Herb Sutter has prompted a variant of the Pimpl idiom that kind of goes this way, as have others. Linus was proudly showing how C was better than C++ by virtue of being able to do something much like this, but of course you can accomplish this in C++, of course there are some down sides that accompany it. Most importantly with C++ you can much more easily provide more robust compile time type safety with little or no runtime overhead.