[cfe-users] [cfe-dev] Warnings for implicit constructors and wrong usage of auto

John McCall via cfe-users cfe-users at lists.llvm.org
Tue May 15 16:01:32 PDT 2018


> On May 15, 2018, at 6:05 PM, George Karpenkov via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> 
> +cfe-dev
> 
> Hi Andrea,
> 
> I think you might get more luck asking on the cfe-dev mailing list.
> 
> George
> 
>> On May 15, 2018, at 1:15 PM, Andrea Arteaga via cfe-users <cfe-users at lists.llvm.org <mailto:cfe-users at lists.llvm.org>> wrote:
>> 
>> Dear all,
>> Recently, my team suffered from a bug due to a double bad usage of C++.
>> 
>> We have a function returning a reference to an object:
>> 
>>     Object& GetObject();
>> 
>> Sometimes we use this function like this:
>> 
>>     auto obj = GetObject();
>> 
>> This triggers a copy of the object, which we really don't mean. The two problems are:
>> 1. Object does not delete the copy constructor, nor does it declare one. We have a policy of never using implicitly-declared constructors, we either use `=delete` or `=default`. Nevertheless we missed this class.

Implicitly-defined copy constructors are ubiquitous in idiomatic C++.  Maybe that's not true in your project, but still, this seems too special-case for the compiler.  Maybe a linter that has a more sophisticated model of what code is yours vs. the standard library.

>> 2. A reference is demoted to a rvalue due to the usage of `auto` instead of `auto&`.

This is a more reasonable thing to try to warn about.  I have two concerns:
  - I don't know a reasonable way to suppress the warning if you really do want to load from the l-value.
  - I have a non-specific worry that it'll disrupt some important idiom that I'm just not thinking of.
But those are concerns that we could explore during iterative design and implementation.

John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20180515/48a32a35/attachment.html>


More information about the cfe-users mailing list