C++ Logo

sg10

Advanced search

Re: [SG10] P0304: constexpr addressof

From: Agustín Bergé <agustinberge_at_[hidden]>
Date: Mon, 14 Mar 2016 09:33:46 -0300
On 3/14/2016 12:48 AM, Gabriel Dos Reis wrote:
> I suppose there is no hope to get people write simple code? :-)

Is that a contrived way of showing your support for the macro-enabled
simple case and thus a feature-test macro? If so, thank you.

Regards,
-- 
Agustín K-ballo Bergé
http://talesofcpp.fusionfenix.com
> -------- Original message --------
> From: Richard Smith <richard_at_[hidden]>
> Date: 3/13/2016 8:47 PM (GMT-08:00)
> To: Agustín Bergé <agustinberge_at_[hidden]>
> Cc: "features_at_[hidden] (features_at_[hidden])"
> <features_at_[hidden]>
> Subject: Re: [SG10] P0304: constexpr addressof
>
> On Sun, Mar 13, 2016 at 6:29 PM, Agustín Bergé <agustinberge_at_[hidden]>
> wrote:
>  > On 3/13/2016 10:16 PM, Richard Smith wrote:
>  >>
>  >> On Fri, Mar 11, 2016 at 11:30 AM, Daniel Krügler
>  >> <daniel.kruegler_at_[hidden]> wrote:
>  >>>
>  >>> 2016-03-11 20:27 GMT+01:00 Agustín Bergé <agustinberge_at_[hidden]>:
>  >>>>
>  >>>> On 3/11/2016 4:14 PM, Nelson, Clark wrote:
>  >>>>>
>  >>>>> Agustin raised the question of whether there should be a macro to
>  >>>>> indicate the presence of the fix for LWG2296. I made a couple of
>  >>>>> replies suggesting that, to me, the answer is not obviously "yes".
>  >>>>>
>  >>>>> So far, no one (not even Agustin) has made any further comment.
>  >>>>
>  >>>>
>  >>>> I said that if there's no macro I'd prefer to leave the code broken.
>  >>>> That should have implied my motivation is weak. I think a macro
> fits the
>  >>>> bill, but if lacks in popularity I'm not going to push for it.
>  >>>
>  >>>
>  >>> I tentatively agree, I see no need for a feature macro here.
>  >>
>  >>
>  >> I can imagine a macro being useful:
>  >>
>  >> struct optional {
>  >>    bool b;
>  >>    T t;
>  >>    constexpr T *get() {
>  >>      return b ?
>  >> #if __cpp_constexpr_addressof
>  >>        std::addressof(t)
>  >> #else
>  >>        &t
>  >> #endif
>  >>        : nullptr;
>  >>    }
>  >> };
>  >
>  >
>  > That's my use case :)
>  >
>  >> The idea here is that it's more important that get() is constexpr than
>  >> that it works if there's an overloaded operator&, so addressof is only
>  >> used if it would actually work in a constant expression. Achieving
>  >> this result with SFINAE seems difficult.
>  >
>  >
>  > This appears to do the trick, but there's a chance that it is ill-formed:
>  >
>  >     #if 0
>  >     using std::addressof;
>  >     #else
>  >     template <typename T>
>  >     constexpr T* addressof(T& v) { return &v; }
>  >     #endif
>  >
>  >     struct is_addressof_constexpr {
>  >         static int _probe;
>  >
>  >         template <typename T, T* = addressof(_probe)>
>
> If you use "std::addressof" instead of "addressof", I think this
> default template argument has no valid instantiations in C++14, so is
> ill-formed (no diagnostic required). If you don't qualify it, it can
> find the wrong addressof via ADL.
>
>  >         static std::true_type check(T);
>  >
>  >         static std::false_type check(...);
>  >
>  >         static constexpr bool value =
>  >             decltype(check(0))::value;
>  >     };
>  >
>  >     static_assert(is_addressof_constexpr::value, "");

Received on 2016-03-14 13:34:13