|
Boost : |
Subject: Re: [boost] [offtopic] C++11 useful trick
From: Roland Bock (rbock_at_[hidden])
Date: 2012-07-03 03:50:07
On 2012-07-02 19:29, Eric Niebler wrote:
> Yeah, it's booo-ring. ;-) Also, it needlessly instantiates O(N)
> templates. And why write both a function template *and* a class template
> when the function template alone will do? Now you have to maintain two
> computations in parallel. I hate writing metafunctions to compute things
> the compiler already knows. And in less trivial examples, the
> metafunction wouldn't be so easy to write. BTW, only the implementation
> of back needs to go in a struct. The user-visible function could fwd to it.
Boring? Hey! :-)
The other arguments are certainly valid, though. I used the attached
versions to compare compilation times:
Using clang with -O3
Eric:
real 0m0.471s
user 0m0.420s
sys 0m0.030s
Roland:
real 0m0.512s
user 0m0.470s
sys 0m0.020s
Obviously the measurement is rather crude and imprecise, but your
version definitely compiles faster. And it is also much shorter and
easier to setup for such a comparison :-)
> This is just a trivial example of the technique. I've been doing a lot
> of C++11 making use of automatic type deduction via decltype+trailing
> return types. This issue comes up fairly often for me, so finding a
> solution to this problem was satisfying and greatly simplified my code.
I'll look through some of the code I've written recently. I guess I can
apply this trick here and there.
Thanks for sharing!
Regards,
Roland
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk