C++ Weekly - Ep 423 - Complete Guide to Attributes Through C++23

Поделиться
HTML-код
  • Опубликовано: 1 окт 2024

Комментарии • 31

  • @sinom
    @sinom 5 месяцев назад +22

    carries_dependency is the one attribute where I have absolutely no idea what it means or does was kinda hoping this video would explain it but I guess I'm not the only one who has problems with understanding it

    • @user-ue9uc7fd1q
      @user-ue9uc7fd1q 5 месяцев назад +1

      Isn't carries_dependency more or less deprecated anyway? It only applies to memory order consume, which is not implemented by any compiler and instead lifted to "acquire " . Additional since C++17 the standard says: "The specification of release-consume ordering is being revised, and the use of memory_order_consume is temporarily discouraged. "

    • @TheMrKeksLp
      @TheMrKeksLp 5 месяцев назад

      And it not producing any diagnostic if you misuse it shows the standards guys don't understand it either. Seriously how in the absolute f can you standardize something which you yourself have no idea how its supposed to work

    • @TsvetanDimitrov1976
      @TsvetanDimitrov1976 5 месяцев назад

      @@TheMrKeksLp It's a very hard problem, even a trivial optimization could potentially break the ordering.

    • @cppweekly
      @cppweekly  5 месяцев назад +1

      Yeah... sorry about that :D

    • @TheMrKeksLp
      @TheMrKeksLp 5 месяцев назад

      @@TsvetanDimitrov1976 Not wrong, but sometimes the c++ standard guys should just NOT standardize it then :D

  • @N....
    @N.... 5 месяцев назад +6

    9:24 It is annoying, but you can use [[msvc::no_unique_address]] instead and it does work as expected, so I typically use a macro based on compiler detection.

  • @PeterStinklage
    @PeterStinklage 5 месяцев назад +5

    It'll be an interesting experiment to see if [[assume]] the length of a std::vector is a multiple of 8 or 16 helps get range-based code vectorized

    • @ohwow2074
      @ohwow2074 5 месяцев назад

      MSVC STL doesn't multiply by 2 though. It multiplies by 1.5 as far as I know.

    • @oracleoftroy
      @oracleoftroy 5 месяцев назад +1

      @@ohwow2074 Different issue. Vectorization depends on length and alignment of the data, not how much additional capacity there might be.

  • @theicebeardk
    @theicebeardk 5 месяцев назад +3

    I mainly use [[maybe_unused]] in front of RAII lock objects where I do not always want the type to be maybe_unused but in many simple cases I do. So I often have: [[maybe_unused]] acquire_t lock(resource);

  • @PedroOliveira-sl6nw
    @PedroOliveira-sl6nw 5 месяцев назад +4

    Already said this somewhere, using a macro like M_EXPECT( condition ) that converts to assert( condition ) in DEBUG, and assume( condition ) in RELEASE is something I am looking for to try.

  • @VoidloniXaarii
    @VoidloniXaarii 5 месяцев назад +1

    Thank you very much for this amazing compilation ❤ so great to have them all together like this together with your insights about importance and frequency

  • @mudkiptheengineer5339
    @mudkiptheengineer5339 5 месяцев назад

    worth noting that nodiscard also supports a message, and I believe there is something like always_inline, but maybe that predates C++11? 🤔

  • @TsvetanDimitrov1976
    @TsvetanDimitrov1976 5 месяцев назад

    Very interesting, do the [[likely]]/[[unlikely]] also generate branch hints(on cpus that still support them, last time i used something like that was on ps3)

    • @simonhrabec9973
      @simonhrabec9973 5 месяцев назад +1

      I think the likely/unlikely is primarily (maybe solely) about how the code is ordered (which may affect the instruction cache/cache misses).

    • @cppweekly
      @cppweekly  5 месяцев назад +1

      This is entirely compiler dependent as to what they do or don't do.

  • @v-for-victory
    @v-for-victory 5 месяцев назад +2

    🎯 Key Takeaways for quick navigation:
    00:00 *🎙️ Introduction to Attributes*
    - Overview of upcoming discussion on attributes in C++ up to C++23.
    - Mention of a C++ Best Practices class being taught by the host.
    - Introduction to the first attribute [[noreturn]] and its purpose in signaling that a function will not return.
    01:23 *🏷️ Understanding [[carries_dependency]] Attribute*
    - Explanation of the [[carries_dependency]] attribute and its use cases.
    - Highlighting the poorly understood nature of the attribute.
    - Discussion on the requirement of where the attribute must appear in code for it to be valid.
    04:16 *🚨 Handling Deprecated Functions*
    - Overview of the [[deprecated]] attribute and its purpose in flagging deprecated functions.
    - Explanation of how using deprecated functions triggers warnings, potentially leading to hard compile failures.
    - Discussion on marking functions as deprecated to guide users towards safer alternatives.
    05:12 *🛑 Understanding [[fallthrough]] in Switch Statements*
    - Explanation of the [[fallthrough]] attribute and its role in switch statements.
    - Demonstration of how the attribute helps prevent common bugs in switch statements.
    - Discussion on intentionally using [[fallthrough]] to indicate intended behavior.
    06:34 *📜 Signifying Intent with [[nodiscard]] and [[maybe_unused]]*
    - Overview of the [[nodiscard]] attribute and its use in signaling that a function's return value should not be ignored.
    - Explanation of how [[nodiscard]] can also be applied to types.
    - Introduction of [[maybe_unused]] and its role in addressing unused variables.
    08:05 *📊 Providing Optimization Hints with [[likely]] and [[unlikely]]*
    - Discussion on the[[likely]] and [[unlikely]] attributes and their impact on compiler optimizations.
    - Demonstration of how these attributes influence compiler behavior based on branch likelihood.
    - Highlighting their role in optimizing code performance.
    09:28 *🔄 Optimizing Memory Layout with [[no_unique_address]]*
    - Explanation of [[no_unique_address]] attribute and its purpose in optimizing memory layout.
    - Demonstration of collapsing empty objects within a struct.
    - Mention of Visual Studio's handling of [[no_unique_address]].
    11:50 *🔍 Making Assumptions with [[assume]]*
    - Introduction to the [[assume]] attribute and its role in allowing the compiler to make assumptions about values during execution.
    - Demonstration of how [[assume]] influences generated code.
    - Cautionary note on the potential for [[assume]] to lead to undefined behavior.
    Made with HARPA AI

  • @MaikeLDave
    @MaikeLDave 5 месяцев назад +1

    Noreturn and void are redudant?

    • @isaactfa
      @isaactfa 5 месяцев назад +12

      No, void just means there's no _return value_. [[noreturn]] means the function will not return control flow to the caller.

    • @sinom
      @sinom 5 месяцев назад +4

      He was a bit unclear at that part. Any code you write after calling a noreturn function is considered to be dead code because that function will not be left through a return, only through a long jump or through exiting the program. With void everything you write after the function call just gets executed after the function returns.

  • @nabilandadamslaboratory3422
    @nabilandadamslaboratory3422 5 месяцев назад +1

    When will C++ have built in support for networking?

    • @ohwow2074
      @ohwow2074 5 месяцев назад +2

      C++26

    • @JanFlunher
      @JanFlunher 5 месяцев назад +2

      Last time I checked, if I remember correctly, it was meant to be implemented with the new concepts of senders/receivers. They didn't want to just make another wrapper over OS sockets. It'll probably take a while (hopefully C++26, but who knows...). But it'll not happen until the senders/receivers and/or a proper asynchronous API/*the* async API is done. Look into P2300 proposal.

    • @cppweekly
      @cppweekly  5 месяцев назад

      I would just use ASIO today if you really want networking. Whatever version gets included in C++ will end up out of date quickly as it can only be fixed / updated every 3 years.