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

Поділитися
Вставка
  • Опубліковано 25 гру 2024

КОМЕНТАРІ • 31

  • @VoidloniXaarii
    @VoidloniXaarii 8 місяців тому +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

  • @sinom
    @sinom 8 місяців тому +24

    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 8 місяців тому +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 8 місяців тому +1

      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 8 місяців тому

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

    • @cppweekly
      @cppweekly  8 місяців тому +2

      Yeah... sorry about that :D

    • @TheMrKeksLp
      @TheMrKeksLp 8 місяців тому +1

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

  • @N....
    @N.... 8 місяців тому +7

    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 8 місяців тому +6

    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 8 місяців тому

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

    • @oracleoftroy
      @oracleoftroy 8 місяців тому +1

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

  • @theicebeardk
    @theicebeardk 8 місяців тому +5

    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 8 місяців тому +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.

  • @v-for-victory
    @v-for-victory 8 місяців тому +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

  • @mudkiptheengineer5339
    @mudkiptheengineer5339 8 місяців тому

    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 8 місяців тому

    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 8 місяців тому +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  8 місяців тому +1

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

  • @MaikeLDave
    @MaikeLDave 8 місяців тому +1

    Noreturn and void are redudant?

    • @isaactfa
      @isaactfa 8 місяців тому +13

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

    • @sinom
      @sinom 8 місяців тому +5

      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 8 місяців тому +1

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

    • @ohwow2074
      @ohwow2074 8 місяців тому +2

      C++26

    • @JanFlunher
      @JanFlunher 8 місяців тому +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  8 місяців тому

      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.