C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Automatic perfect forwarding is possible and not too complicated

From: Bo Persson <bo_at_[hidden]>
Date: Thu, 10 Apr 2025 15:05:59 +0200
On 2025-04-10 at 14:58, Frederick Virchanza Gotham via Std-Proposals wrote:
> On Thu, Apr 10, 2025 at 12:20 PM Giuseppe D'Angelo wrote:
>>
>> template <std::range T>
>> void f(T &&& arg)
>> {
>> auto b = ranges::begin(arg);
>> auto e = ranges::end(arg);
>>
>> use(b, e);
>>
>> assert(check(arg));
>> }
>
>
> If NDEBUG is defined, the above code becomes:
>
> template <std::range T>
> void f(T &&arg)
> {
> auto b = ranges::begin(arg);
> auto e = ranges::end( forward<T>(arg) );
>
> use( b, e );
>
> (void)0;
> }
>
> Otherwise it becomes:
>
> template <std::range T>
> void f(T &&arg)
> {
> auto b = ranges::begin(arg);
> auto e = ranges::end(arg);
>
> use( b, e );
>
> check( forward<T>(arg) ) || __assert_failure( "check(arg)");
> }
>
> So when compiling in Debug mode, you'll occasionally get an
> L-value-accepting function being called instead of an
> R-value-excepting function. This isn't a big deal.
>

This is a very big deal where I come from. Having code do different
things in test and production environments is a big no-no. What have we
tested?!

Received on 2025-04-10 13:06:12