
on Thu Apr 05 2012, Andrzej Krzemienski <akrzemi1-AT-gmail.com> wrote:
For the end users of meta-programming library (at least those interested in type transformations), I believe that alias templates offer a significant improvement to the interface: http://akrzemi1.wordpress.com/2012/03/19/meta-functions-in-c11/
Yeah... I started doing a bunch of that, but I am not yet convinced it's an overall improvement. One thing you emphatically *don't* get with that formulation is lazy evaluation. remove_pointer<int> is valid until you instantiate it but RemovePointer<int> is not, and the former is often quite useful. Also, you can't use metafunction forwarding—which also eliminates vast amounts of typing—with RemovePointer. Also, you end up needing to define more names than you otherwise would (e.g. both remove_pointer and RemovePointer). My standard approach when doing this has been to name them remove_pointer_ and remove_pointer, so at least they have an obvious and mechanical relationship.
I guess my understanding of the name "meta-programming" might be a bit different, and term "end user" might have been too much a mental shortcut, but let me share my view of someone that doesn't want to go to deep into the meta-programming but still needs to use the simple forms thereof (like type traits). This is a somewhat simplified version of what I was facing recently. I need a function that perfect-forwards the argument but returns by value: template <class T> T fun(T&& v); The above is incorrect, because T may be deduced to be an lvalue reference. But be sympathetic, perfect-forwarding is already hard enough to acquire. I need type trait std::decay: template <class T> std::decay<T> fun(T&& v); But this does not work! I forgot "::type". Let's try again: template <class T> std::decay<T>::type fun(T&& v); No. It still doesn't work. And this is the worst part: I forgot the "typename". Having to type this "typename" is really terrible and just scares off many people. If you do not mind it, it only means that you got used to it; but people should not be forced to get used to too many gotchas. Alias templates offer a simplification that can improve the "teachability" of type traits. You may not consider type traits a proper meta-programming, and all I say might be a bit off topic. But if we had these alias templates for type traits, it looks to me, it would make a soft introduction to meta-programming for the newbies. Regards, &rzej