As many of you are aware, Chromium lists the allowed subsets of C++11/14. Since not everyone keeps constant track, here are the features that have been allowed since the last email I sent ~10 months ago. There are a lot!--
- std::make_unique<>(), which should replace all existing usage of base::MakeUnique<>() and WTF::MakeUnique<>(). (And, in general, should be used in preference to bare new.)
- Binary literals, so masks and bit-based constants can be written as 0b11010001 instead of 0xD1, where doing so would be more readable.
- Literal separators in numeric constants, so that long constants like 0b10000000100000001000000010000000 can be broken into pieces like 0b10000000'10000000'10000000'10000000.
- constexpr on more complex code, including code with conditionals and loops. This allows marking more simple functions as constexpr.
- Aggregate initialization of structs with default members, which allows structs with default values specified for some/all members to still be initialized with an aggregate of values. This may be useful for unit tests which define structs to hold test case data, then initialize arrays of such structs, where many cases in the array have some identical fields.
- std::next() and std::prev(), to increment and decrement copies of iterators by some value; these can be useful with non-random access types such as std::list, or when writing templated code that deals with different kinds of containers.
- The no-parameter form of std::less<>, a function object for doing comparisons which can avoid some unnecessary temporaries in e.g. heterogeneous lookup.
- std::shrink_to_fit(), which makes a non-binding request that a container reduce its capacity to match its size.
- Tuple addressing by type, which allows calling std::get<> with a type parameter instead of a numeric one when doing so is unambiguous. This can be useful for improving the readability of code which conceptually wants to say "get the string from this tuple" instead of "get the third element".
- std::cbegin(), std::cend(), std::crbegin(), and std::crend(), which are constant counterparts to std::begin() and friends (which work like begin() member functions but can operate on e.g. array types as well).
- The alignas() specifier and alignof() operator, which allow setting and querying the alignment for types. Note that there are some usage caveats around alignas(); see the doc.
- Functionality in <atomic>, which provides low-level atomic transaction functionality. Very few people should need to use this directly; prefer higher-level synchronization primitives.
- std::integer_sequence<>, which represents a sequence of integers as a type. This is useful for certain kinds of template programming that I think relate to variable numbers of arguments? (I dunno, I've never used this.)
Additionally, there are some changes to recommended best practices:
- Move constructors should be declared noexcept where possible, since despite Chromium compiling with exceptions disabled, this affects things like whether std::vector can move instead of copy these objects.
- With the removal of libstdc++, various features (e.g. std::get, std::map::emplace, <type_traits>, etc.) no longer have usage caveats around library bugs.
Finally, some features have been banned:
- std::chrono literals, which are banned since <chrono> is banned.
- thread_local, to mark a variable as thread-local, due to some surprising effects. This may be allowed again in the near future, however.
- Function return type deduction, which allows a function to be declared as returning auto (without a trailing return type). This can currently cause infinite loops in clang; once that bug is fixed, it will likely be allowed.
- Generic (or "polymorphic") lambdas, which allow the use of auto in declaring lambda parameter types. As with function return type deduction, this can cause clang bugs and may be allowed when those are fixed.
As always, to propose allowing/banning your favorite feature, send mail to c...@chromium.org.PK
--
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 view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CAAHOzFBPmS6Drun0dg5sy1Vw7W35rLc%2BF-oO32FZsPO0rGh7_Q%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAMGbLiEA7nVT1L2Gt1GkXLNAzmoCtuQJpcDdBpL3aW98c_RNvQ%40mail.gmail.com.
This is a reply to an 1.5 year old email, but there's currently a mass-clang-tidy rewrite to add "noexcept" to all move ctors. I pushed back on this and several people sent me a link to the email this is replying to, so I wanted to open this up for discussion.This mail says "move ctors should be declared noexcept", but https://google.github.io/styleguide/cppguide.html#noexcept actually says "Specify noexcept when it is useful and correct." -- that sounds more like "it's ok to use this when it makes sense", not "you should use this".I think doing a mass rewrite isn't a great idea, because:- There's hope to one day have the same semantic effect automatically when building with -fno-exceptions (e.g. https://reviews.llvm.org/D62228), and at that point this is just busy work
- gcc and clang have different requirements for noexcept, and auto-adding this everywhere increases the cost of keeping the gcc build going
- it adds visual noise
- most of the time it has no benefitIt's fine to add noexcept in cases where it provides measurable perf benefits, but I don't think we should do a mass codebase rewrite. We also should follow the google style guide that says "use when useful and correct", not "should".
On Thu, Sep 5, 2019 at 10:43 AM Nico Weber <tha...@chromium.org> wrote:This is a reply to an 1.5 year old email, but there's currently a mass-clang-tidy rewrite to add "noexcept" to all move ctors. I pushed back on this and several people sent me a link to the email this is replying to, so I wanted to open this up for discussion.This mail says "move ctors should be declared noexcept", but https://google.github.io/styleguide/cppguide.html#noexcept actually says "Specify noexcept when it is useful and correct." -- that sounds more like "it's ok to use this when it makes sense", not "you should use this".I think doing a mass rewrite isn't a great idea, because:- There's hope to one day have the same semantic effect automatically when building with -fno-exceptions (e.g. https://reviews.llvm.org/D62228), and at that point this is just busy work
- gcc and clang have different requirements for noexcept, and auto-adding this everywhere increases the cost of keeping the gcc build going
- it adds visual noise
- most of the time it has no benefitIt's fine to add noexcept in cases where it provides measurable perf benefits, but I don't think we should do a mass codebase rewrite. We also should follow the google style guide that says "use when useful and correct", not "should".I echo K's response that the Google Style Guide intends to imply that "noexcept is always useful on move constructors", and thus this is always "useful and correct" for us. I don't think D62228 moots this argument completely, since the optimization there does not apply to non-clang or if we ever enable exceptions (e.g. in a future C++XX world where someone has created a lighter-weight exception system). It also does not help any other optimizations anyone writes now or in the future that check the noexcept state (this is my way of saying that I agree with the bug comment that it's wrong for upstream to narrowly solve this for std::vector only
). It also makes me uncomfortable to omit semantic information because of a clang- or libc++-only change; in principle I believe we should aim to avoid relying on implementation-specific behavior if it's feasible to do so. The cross-platform, wider-C++-world way of achieving what you want is to mark move constructors noexcept.
I'm interested in the degree to which this concretely makes life harder for gcc. Do we have move constructors that violate some of gcc's noexcept requirements?
On Thu, Sep 5, 2019 at 4:01 PM 'Peter Kasting' via cxx <c...@chromium.org> wrote:I echo K's response that the Google Style Guide intends to imply that "noexcept is always useful on move constructors", and thus this is always "useful and correct" for us. I don't think D62228 moots this argument completely, since the optimization there does not apply to non-clang or if we ever enable exceptions (e.g. in a future C++XX world where someone has created a lighter-weight exception system). It also does not help any other optimizations anyone writes now or in the future that check the noexcept state (this is my way of saying that I agree with the bug comment that it's wrong for upstream to narrowly solve this for std::vector only(I agree with that too, but in the sense that I think we should find a way to make -fno-exceptions implicitly add noexcept on all functions in the compiler.)
). It also makes me uncomfortable to omit semantic information because of a clang- or libc++-only change; in principle I believe we should aim to avoid relying on implementation-specific behavior if it's feasible to do so. The cross-platform, wider-C++-world way of achieving what you want is to mark move constructors noexcept.We're already not writing exception safe code -- which we shouldn't, since we don't need it and since it's more work.
If we ever want to turn on some hypothetical future exception mode, that will need lots of prep work and planning -- sprinkling "noexcept" over the code base would be one of the smaller work items. In fact, adding it now even makes that hypothetical future step harder -- if we mechanically add it everywhere now, that might no longer be true in a future where some of our code might throw.
Since there's no standard for "C++ without exceptions", relying on what compilers do seems the only thing we can do wrt exceptions.
I'm interested in the degree to which this concretely makes life harder for gcc. Do we have move constructors that violate some of gcc's noexcept requirements?There's a steady trickly of noexcept-related changes on https://crbug.com/819294
Thanks for the thought-through responses, Peter.Would you still want to add noexcept if -fno-exceptions already had the "implicit noexcept" behavior?
On Thu, Sep 5, 2019 at 1:45 PM Nico Weber <tha...@chromium.org> wrote:Thanks for the thought-through responses, Peter.Would you still want to add noexcept if -fno-exceptions already had the "implicit noexcept" behavior?Probably, because that's still a clang-only sort of thing. Maybe not if all compilers did this.
I know the standard considers "no exceptions" mode anathema, but if it ever laid out some guidelines for a "no exceptions" mode and this was one of them, that'd immediately convince me to remove all "noexcept"s from our codebase :)I am also not going to die on this hill. When the benefits and costs here are this small, I am happy to go with a consensus decision either way.
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAMGbLiEHK8itak%2Bc0ZnmaW7Hn%3DDHrdVrK9UJ%3D%3DB7n4UQMiHjXQ%40mail.gmail.com.
A nitpicky point of clarification:
-fno-except doesn't mean there are no exceptions.
Imagine statically linking a prebuilt library. Even if we build our code with -fno-except, the library might still throw.
If we (implicitly) mark our function as noexcept then our call into the library will now have a compiler-added catch-all which calls std::terminate.
Implicitly marking functions as noexcept is a change in behavior. I doubt compilers will do this.
I realize in Chrome we compile everything and won't be statically linking to throwing libraries. But we are still beholden to the compiler which can't change behavior for us.
Likewise, implicitly returning true from the noexcept operator is a change in behavior (see std::vector). So I doubt compilers will do that as well.
With all of that said, the only way to not rely on compilers changing our code's behavior (a dangerous notion
) is for us to explicitly tell the compiler a function is indeed noexcept. Said another way, noexcept != -fno-except -- they are different tools that accomplish different things.--On Thu, Sep 5, 2019 at 2:04 PM Nico Weber <tha...@chromium.org> wrote:--On Thu, Sep 5, 2019 at 4:53 PM 'Peter Kasting' via cxx <c...@chromium.org> wrote:On Thu, Sep 5, 2019 at 1:45 PM Nico Weber <tha...@chromium.org> wrote:Thanks for the thought-through responses, Peter.Would you still want to add noexcept if -fno-exceptions already had the "implicit noexcept" behavior?Probably, because that's still a clang-only sort of thing. Maybe not if all compilers did this."All" being gcc and msvc, or more?If we ever implement this in clang, I think it's more likely than not that gcc grows a similar feature. MSVC doesn't really have a -fno-exceptions mode.I know the standard considers "no exceptions" mode anathema, but if it ever laid out some guidelines for a "no exceptions" mode and this was one of them, that'd immediately convince me to remove all "noexcept"s from our codebase :)I am also not going to die on this hill. When the benefits and costs here are this small, I am happy to go with a consensus decision either way.I'm also happy to withdraw my concern if general agreement is that we want explicit noexcepts on move ctors.
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAMGbLiEHK8itak%2Bc0ZnmaW7Hn%3DDHrdVrK9UJ%3D%3DB7n4UQMiHjXQ%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAPHsw34E5OMbF%2BXLi-%2BdgDeL6T6eaYTs8Atn0CNLpDBW_YDuGw%40mail.gmail.com.
Imagine statically linking a prebuilt library. Even if we build our code with -fno-except, the library might still throw.
If we (implicitly) mark our function as noexcept then our call into the library will now have a compiler-added catch-all which calls std::terminate.
Likewise, implicitly returning true from the noexcept operator is a change in behavior (see std::vector). So I doubt compilers will do that as well.
On Thu, Sep 5, 2019 at 4:53 PM 'Peter Kasting' via cxx <c...@chromium.org> wrote:On Thu, Sep 5, 2019 at 1:45 PM Nico Weber <tha...@chromium.org> wrote:Thanks for the thought-through responses, Peter.Would you still want to add noexcept if -fno-exceptions already had the "implicit noexcept" behavior?Probably, because that's still a clang-only sort of thing. Maybe not if all compilers did this."All" being gcc and msvc, or more?
If we ever implement this in clang, I think it's more likely than not that gcc grows a similar feature. MSVC doesn't really have a -fno-exceptions mode.
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAPHsw35Azev5qX3O%2BM23sZ-i4buD%3DRfrMwmTH%2Bt_DP_jm1jo%3DQ%40mail.gmail.com.
Hi, all!
Strong +1 for "we should find a way to make -fno-exceptions implicitly add noexcept on all functions in the compiler"
(up to adding -fseriously-no-except).
On Fri, Sep 6, 2019 at 4:42 PM <lo...@chromium.org> wrote:Hi, all!
Strong +1 for "we should find a way to make -fno-exceptions implicitly add noexcept on all functions in the compiler"
(up to adding -fseriously-no-except).I think someone has already mentioned this somewhere, but let me explain in my words why adding such an option is difficult and controversial.Normally, object files generated with different compile options should be able to get linked happily. The auto-noexcept option will give you the different semantics of C++ code relying std::is_nothrow_constructible and similar, which are actually used in many std code. Introduction of the option would make the semantics of the same function differ in different translation units. This is very bad, and linking such object files result undefined behavior -- practically, the linker may arbitrarily throw away object code from some translation units, or the final binary may contain multiple instances of inconsistent code that comes from the same function (due to inlines).So, if you want to use that option, you basically need to compile all files with the flag set. However this is generally impractical, because you at least need to link standard C++ library to your binary, and the standard library is usually built with exceptions enabled. This also means you can't use any dynamic link library (that uses C++) available in your system.We, however, compile almost all the code by ourselves (modulo external DLLs), so this might be solvable. However, from compiler developers' perspective, the option makes no sense.
Almost nobody uses custom-built standard library. The option will result undefined behavior more than 99% of the time. Adding such an option would give you an endless queue of bugs from people who have innocently used that option.Thus, simply adding the auto-noexcept flag is a big no-no. To overcome this issue, I think someone smart needs to come up with the ABI (on every platform!) that can accommodate object code coming from both settings, but I doubt that's practically possible.
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/710aeb1c-90ad-466c-9d9f-2cbb29966a7a%40chromium.org.
This understanding is not quite correct. std::vector commonly uses std::move_if_noexcept during its resize operation, which is a copy if the value (!is_nothrow_move_constructible && is_copy_constructible), and a move otherwise.
In particular this means the move constructor of both copyable and moveable should be marked noexcept, as otherwise vector will copy.
On Mon, Mar 9, 2020 at 8:58 AM Jan Wilken Dörrie <jdoe...@chromium.org> wrote:This understanding is not quite correct. std::vector commonly uses std::move_if_noexcept during its resize operation, which is a copy if the value (!is_nothrow_move_constructible && is_copy_constructible), and a move otherwise.I was asking about move-only types, where falling back to a copy isn't an option. Does this not mean that a move-only type can only be stuffed into a vector if it can be moved (via move assign or move construct) without an exception being thrown? Or does vector have some other path where it'll do the move and let exceptions fly?
In particular this means the move constructor of both copyable and moveable should be marked noexcept, as otherwise vector will copy.This is where the style guide makes things muddy: "You may use noexcept when it is useful for performance if it accurately reflects the intended semantics of your function". Where do we draw the line? Some (not me) might say "don't use noexcept unless you can demonstrate a performance hit by not using it."But wait! I just noticed that the style guide follows that with "You can assume that noexcept on move constructors has a meaningful performance benefit." I read that as "always put noexcept on move ctors as long as it's actually correct." Should we not be doing this? If no, why not?
Note that your final paragraph has circled around to the original cause of this thread: a mass rewrite to do this everywhere and a debate over wherever we should be doing it. Nico makes the case for "why not". My position is still that we should do this everywhere.PK
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAKQnr0QY0KBgbn5%2BCZ8GQZYMeKD0MKPxGem8mH6cM47FPtSpsQ%40mail.gmail.com.
Due to the lack of exceptions in Chromium, I agree that there's no practical effect (I believe the compiler will do the right thing when exceptions are turned off), so this seems purely a matter of consistency. On that basis, it seems like a good practice to adopt, but it's not urgent or anything.
I threw together a Compiler Explorer link if anyone wants to play around with this: https://godbolt.org/z/IBvZTnAt least for my example, and with x86-64 clang-trunk, noexcept makes a difference with exceptions (as you'd expect), and makes no difference with -fno-exceptions (also as you'd expect, hopefully).I poked around a bit with the gcc vs. clang behavior, but wasn't able to trigger the compilation error; I'm assuming I didn't hit the right set of conditions to trigger it.