Hi,TLDR, should we disallow auto to deduce a raw pointer?Details:We've been using auto for a while in the codebase now and one of the unwritten rules that had some consensus was that we should not use auto with smart pointers. That discussion happened here:https://groups.google.com/a/chromium.org/forum/#!search/%5Bchromium-dev%5D$20auto$20keyword/chromium-dev/5-Bt3BJzAo0/Z5l6XJkJKo4JOne of the main concerns was that when one seesauto foo = GetFoo();foo->bar();it isn't clear whether foo is a raw pointer or a smart pointer. As a result we've been working to write a clang plugin that would enforce the developers to not use auto to deduce a raw pointer. Specifically,auto foo = GetFoo();foo->bar();would only compile if GetFoo() returns a smart pointer. If it returns a raw pointer, then it would have to be written like so:auto* foo = GetFoo();foo->bar();pkasting@ rightfully pointed out that this is something that would affect the style guide. Since the current style guide is silent on the issue, he encouraged me to write this email.So the main question is whether we want this to be a rule. Something along the lines of "don't use auto when it would deduce to a raw pointer".
Hi,TLDR, should we disallow auto to deduce a raw pointer?Details:We've been using auto for a while in the codebase now and one of the unwritten rules that had some consensus was that we should not use auto with smart pointers. That discussion happened here:https://groups.google.com/a/chromium.org/forum/#!search/%5Bchromium-dev%5D$20auto$20keyword/chromium-dev/5-Bt3BJzAo0/Z5l6XJkJKo4JOne of the main concerns was that when one seesauto foo = GetFoo();foo->bar();it isn't clear whether foo is a raw pointer or a smart pointer. As a result we've been working to write a clang plugin that would enforce the developers to not use auto to deduce a raw pointer. Specifically,auto foo = GetFoo();foo->bar();would only compile if GetFoo() returns a smart pointer. If it returns a raw pointer, then it would have to be written like so:auto* foo = GetFoo();foo->bar();
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
Perhaps unintentionally, but these examples all feel like they go against the admonitions of the style guide regarding the use of auto - https://google.github.io/styleguide/cppguide.html#auto
On Fri, Jul 22, 2016 at 3:26 PM, Ryan Sleevi <rsl...@chromium.org> wrote:Perhaps unintentionally, but these examples all feel like they go against the admonitions of the style guide regarding the use of auto - https://google.github.io/styleguide/cppguide.html#autoIndeed. If it's not clear whether |foo| is a raw pointer, smart pointer, or something else in:auto foo = GetFoo();...then I believe the style guide says you should not use auto.So if there is a problem here, I think it's that people are wanting to use auto in cases where using it results in inclarity. More to the point: I don't believe saying "clang will enforce using auto* for raw pointers" causes the statement above to become significantly clearer if it wasn't clear enough already.
On Fri, Jul 22, 2016 at 3:57 PM, Peter Kasting <pkas...@chromium.org> wrote:On Fri, Jul 22, 2016 at 3:26 PM, Ryan Sleevi <rsl...@chromium.org> wrote:Perhaps unintentionally, but these examples all feel like they go against the admonitions of the style guide regarding the use of auto - https://google.github.io/styleguide/cppguide.html#autoIndeed. If it's not clear whether |foo| is a raw pointer, smart pointer, or something else in:auto foo = GetFoo();...then I believe the style guide says you should not use auto.So if there is a problem here, I think it's that people are wanting to use auto in cases where using it results in inclarity. More to the point: I don't believe saying "clang will enforce using auto* for raw pointers" causes the statement above to become significantly clearer if it wasn't clear enough already.Can you explain what you mean? If |foo| can not be a raw pointer due to this enforcement in clang, then it's quite clear you have ownership of whatever GetFoo returned, be it a smart pointer or otherwise.
On Fri, Jul 22, 2016 at 4:09 PM, <dan...@chromium.org> wrote:On Fri, Jul 22, 2016 at 3:57 PM, Peter Kasting <pkas...@chromium.org> wrote:On Fri, Jul 22, 2016 at 3:26 PM, Ryan Sleevi <rsl...@chromium.org> wrote:Perhaps unintentionally, but these examples all feel like they go against the admonitions of the style guide regarding the use of auto - https://google.github.io/styleguide/cppguide.html#autoIndeed. If it's not clear whether |foo| is a raw pointer, smart pointer, or something else in:auto foo = GetFoo();...then I believe the style guide says you should not use auto.So if there is a problem here, I think it's that people are wanting to use auto in cases where using it results in inclarity. More to the point: I don't believe saying "clang will enforce using auto* for raw pointers" causes the statement above to become significantly clearer if it wasn't clear enough already.Can you explain what you mean? If |foo| can not be a raw pointer due to this enforcement in clang, then it's quite clear you have ownership of whatever GetFoo returned, be it a smart pointer or otherwise.Having to think as I read the code that "auto foo" can't result in a raw pointer type, not because of anything in the language, but because we happen to have a plugin preventing that, does not fit my definition of unambiguously clear and immediately readable to the degree sufficient to justify using "auto". I believe the style guide sets a high bar for using "auto" and this kind of use doesn't meet it.Compare to Lei's example, where it's obvious from the immediate context exactly what the type of the expression is.
I believe the point is that we currently have a blanket ban on writing auto where it would be a smart pointer,
I would like to think we can lift said blanket ban and just defer to the Google style guide again, which is to say use auto if it's clear.
PK
Anyhow, it sounds like we are much in agreement with the outcome, so that's good.
--
auto* only binds to raw pointers, right? Not unique_ptr or shared_ptr.
--
PK
On Tue, Jul 26, 2016 at 3:59 PM, Peter Kasting <pkas...@chromium.org> wrote:On Tue, Jul 26, 2016 at 3:56 PM, Chris Blume <cbl...@google.com> wrote:auto* only binds to raw pointers, right? Not unique_ptr or shared_ptr.Correct. AFAICT, that was what initially got this started: the sense that "auto*" provided a distinction between deduced raw and smart pointer types.With the clang plugin, it does provide this distinction. Although you're right that this was the original motivation, I think the proposal here stands on its own. I find that it's easy to miss the following construct in a review:for (const auto& foo : collection) {if (foo == some_other_foo_)DoSomething();......foo->SomeStateModifyingFunction();}
On Tue, Jul 26, 2016 at 4:08 PM, Vladimir Levin <vmp...@chromium.org> wrote:On Tue, Jul 26, 2016 at 3:59 PM, Peter Kasting <pkas...@chromium.org> wrote:On Tue, Jul 26, 2016 at 3:56 PM, Chris Blume <cbl...@google.com> wrote:auto* only binds to raw pointers, right? Not unique_ptr or shared_ptr.Correct. AFAICT, that was what initially got this started: the sense that "auto*" provided a distinction between deduced raw and smart pointer types.With the clang plugin, it does provide this distinction. Although you're right that this was the original motivation, I think the proposal here stands on its own. I find that it's easy to miss the following construct in a review:for (const auto& foo : collection) {if (foo == some_other_foo_)DoSomething();......foo->SomeStateModifyingFunction();}I agree that this code is extremely wrong. No author should explicitly-qualify "auto" in a way that doesn't match the underlying type like this. The code above violates the Google Style Guide's rules on confusion IMO.I don't think that's the proposal here, though. Proposing that people qualify auto when possible seems different than reminding people not to over-qualify it in a way that's misleading. Indeed, if there's any link at all between the two, I would worry that asking people to qualify it everywhere might be more likely to lead to the above, rather than less, for two reasons:(1) Being in the habit of writing "const auto&" because you're supposed to could lead you to put it where it doesn't belong.(2) If someone writes this: "for (const auto& x : GetVec())", and the return type of GetVec() is later changed, this could still compile, but change from being innocuous to misleading.
All this assumes the clang check in question wouldn't flag the above code as bad. If it would, I think we should turn that part of the check on regardless of the outcome of the other discussion.
PK