|
Boost : |
Subject: Re: [boost] C++11 Metaprogramming
From: Dave Abrahams (dave_at_[hidden])
Date: 2012-04-03 07:14:48
First, to all who replied, let me say "thank you!" Now, on to the fun
part...
on Sun Apr 01 2012, Mathias Gaunard <mathias.gaunard-AT-ens-lyon.org> wrote:
> On 04/01/2012 04:12 AM, Dave Abrahams wrote:
>>
>> Hi All,
>>
>> I am on the C++Now 2012 schedule giving a talk on metaprogramming in
>> C++11, which is really just supposed to be an overview of the state of
>> the art. I am just at the beginnings of my research for this
>> presentation, having learned a few things and done a few experiments,
>> and it seemed to me foolish not to ask the Boost community for its
>> insights. I'm sure y'all have come up with many neat tricks and
>> techniques. If you'd care to share them here, that would be much
>> appreciated.
>
> From a pure meta-programming perspective, I guess the only real
> addition is variadic templates.
Well, I disagree, but... I'm not going to bother listing all the other
things I think are relevant here because they'll be discussed elsewhere
in the thread...
> However, there is the problem that they're fairly limited and that one
> may not expand them as the arguments of a non-variadic template.
> Since I need to integrate with other libraries and tools, I therefore
> use them very rarely.
I don't understand what you mean by "them" in "expand them." Argument
packs can certainly be expanded in arguments to non-variadic templates.
For example,
template <class...Ts> struct vector {};
template <class Sequence> struct non_variadic {};
template <class...Ts> struct foo : non_variadic<vector<Ts...> > {};
But I'm sure you knew that already. The inability to handle argument
packs directly without wrapping them in something like vector is a real
limitation, though.
> One very useful use of variadic templates however is with function
> templates. They allow to make a function template with 0 arguments,
> which wasn't possible in C++03. This can be used to delay
> instantiation of the function body which can be necessary if name
> resolution needs to happen later.
...which almost seems like cheating ;-)
> This is also possible with the new function template default arguments.
Ah, yes.
> A new possibility with C++11 is the use of SFINAE to test arbitrary
> expressions. I have not found this to be particularly useful in
> practice, however.
Agreed. It's often hard to draw any definite conclusions about user
intentions based on valid expressions, because of false positives.
> Detecting nested types is often good enough.
>
> decltype is very useful. I use it in some meta-programming contexts to
> select a type when I need best-match selection:
> type0 f(input_iterator_tag);
> type1 f(forward_iterator_tag);
> type2 f(bidirectional_iterator_tag);
> typedef decltype(f(typename
> std::iterator_traits<T>::iterator_category())) result;
>
> The only thing we could do before C++11 was return a type with a
> unique size, then associate that size to a type.
Yep.
-- Dave Abrahams BoostPro Computing http://www.boostpro.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk