[c++ question] detecting reference to temporaries

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[c++ question] detecting reference to temporaries

Boost - Dev mailing list
I have a stupid question related to a bug in boost.test. A piece of code
is doing something like:

const value& get() {
   return *internal_iterator; // can be a ref or not to value or another
type
}

The fix is trivial, *but* I want to test it.

Is there any technique that would let me test for reference to
temporaries? The only option I see would be to promote compiler warnings
into error, but maybe there is something there?

Thanks,
Raffi

(complete example here:
https://github.com/boostorg/test/blob/90d577a620e008417af79f5caa25e701eef954bc/include/boost/test/data/monomorphic/join.hpp#L60 
)


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [c++ question] detecting reference to temporaries

Boost - Dev mailing list
> Is there any technique that would let me test for reference to
> temporaries? The only option I see would be to promote compiler warnings
> into error, but maybe there is something there?

AddressSanitizer with -fsanitize-address-use-after-scope
https://github.com/google/sanitizers/wiki/AddressSanitizerUseAfterScope

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [c++ question] detecting reference to temporaries

Boost - Dev mailing list
In reply to this post by Boost - Dev mailing list
czw., 25 paź 2018 o 00:53 Raffi Enficiaud via Boost <[hidden email]>
napisał(a):

> I have a stupid question related to a bug in boost.test. A piece of code
> is doing something like:
>
> const value& get() {
>    return *internal_iterator; // can be a ref or not to value or another
> type
> }
>
> The fix is trivial, *but* I want to test it.
>
> Is there any technique that would let me test for reference to
> temporaries?


Test in one place or anywhere you are using references?

Does it have to work for pre-c++11 compilers? If not you can employ rvalue
references and deleted functions. I am using the following technique for
not binding temporaries to const-references in function arguments:
https://github.com/akrzemi1/explicit/blob/master/doc/lvalue_ref.md
Not exactly the same problem, but maybe the solution could be adapted.

The only option I see would be to promote compiler warnings
> into error, but maybe there is something there?
>

Making use of compiler warnings looks like a reasonable strategy to me.

Regards,
&rzej;

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost