Boost logo

Boost :

Subject: Re: [boost] C++11 Metaprogramming
From: Dave Abrahams (dave_at_[hidden])
Date: 2012-04-04 11:53:39


on Tue Apr 03 2012, Pyry Jahkola <pyry.jahkola-AT-iki.fi> wrote:

> Not to my knowledge (or correct me if it does!). I just acquired this
> habit from the way the C++11 standard library defined constants like
> the following in the FDIS:
>
> namespace std {
> template <class T, T v>
> struct integral_constant {
> static constexpr T value = v;
> typedef T value_type;
> typedef integral_constant<T,v> type;
> constexpr operator value_type() { return value; }
> };
> }

Hm. I don't know. I wonder what the purpose of those four characters
is?

>>> 6) Defining function result and result type at once.
>>
>> Totally. I have used
>>
>> // RETURNS() is used to avoid writing boilerplate "->decltype(x) {
>> return x; }" phrases.
>> //
>> // USAGE: auto function(<arguments>) RETURNS(<some-expression>);
>
> I like the trick of requiring a semicolon here.

It makes editors behave, anyway.

>> Probably it's a good idea to incorporate noexcept: (...)
>
> True. I decided to postpone the use of noexcept until I get other
> things to work, so I'm still missing all the little tricks there are
> with noexcept. (Keen to hear about them in May!)

Well, it certainly can't hurt to add the clause when everything's
deduced anyway.

>>> It can't be used with recursive definitions like here, though:
>>>
>>> // template <typename A, typename B, typename... C>
>>> // auto operator()(A const & a, B const & b, C const &... c) const ->
>>> // RETURNS(mul_()(a * b, c...))
>>
>>> // --> Error: invalid use of incomplete type mul_
>>
>> Heh, try (*this) instead of mul_(). That works until you try to
>> incorporate noexcept as suggested above (at least on GCC 4.7).
>
> Yep, same thing on clang: as soon as I enable noexcept in the
> RETURNS(...) macro, it gives "error: invalid use of 'this' outside of
> a nonstatic member function".
>
>> // ****** workaround ******
>> static mul_ get() noexcept { return mul_(); }
>
> I'm not sure which one is correct here, GCC or Clang, but but this
> trick had a similar result: "error: calling 'get' with incomplete
> return type 'mul_'".
>
> The only alternative way I could fix it was to define a function
> returning a reference, e.g.:
>
> struct mul_ {
> // ...
>
> static mul_ const & get() noexcept;
> };
>
> constexpr mul_ mul = {};
>
> inline mul_ const & mul_::get() noexcept { return mul; }
>
>> very annoying.
>
> Indeed.

I think it's a GCC bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52869

>>> 7) Counted template recursion.
>>
>> Isn't it a bit slicker to do this by creating an argument pack
>> of integers and expanding that with get<Is>(t)... ?
>
> Oh, sure! I wonder how I missed that. Yes, converting a template
> argument pack into an equal-length index sequence is very useful.

Don't feel bad; I "missed it" too, until learned about it from someone
else ;-)

-- 
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