HackMD
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # TL;DR for #CppSan This article is a brief introduction to works mentioned in https://www.reddit.com/r/cpp/comments/9vwvbz/2018_san_diego_iso_c_committee_trip_report_ranges/. ## The One Range Proposal was merged > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0896r3.pdf See https://en.cppreference.com/w/cpp/experimental/ranges ```cpp #include <ranges> using namespace std::ranges; vector<int> ints{0,1,2,3,4,5}; auto even = [](int i){ return 0 == i % 2; }; auto square = [](int i) { return i * i; }; for (int i : ints | view::filter(even) | view::transform(square)) { cout << i << ’ ’; // prints: 0 4 16 } for (int i : iota_view{1, 10}) cout << i << ’ ’; // prints: 1 2 3 4 5 6 7 8 9**** string str{"the quick brown fox"}; split_view sentence{str, ’ ’}; for (auto word : sentence) { for (char ch : word) cout << ch; cout << " *"; } // The above prints: the *quick *brown *fox * // Existing functions in <algorithm> add support // for Ranges // Existing one: template<class InputIterator, class OutputIterator, class UnaryOperation> constexpr OutputIterator transform(InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperation op); // The new ones: constexpr unary_transform_result<I, O> transform(I first1, S last1, O result, F op, Proj proj = Proj{}); // S = Sentinel<I>= constexpr unary_transform_result<safe_iterator_t<Rng>, O> transform(Rng&& rng, O result, F op, Proj proj = Proj{}); // Roughly equivalent to // for (auto& v: rng) // *result++ = F(proj(v)); // By default, Proj = Identity ``` ## Constrained auto > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1141r1.html Before this, Concept can only be used in template type arguments. Now it can be used in parameter/return types. ```cpp void f(Sortable auto x); // can't omit "auto" here Sortable g(); // auto omitted, = Sortable auto g() Sortable x = g(); // = Sortable auto x = g(); ``` ## consteval function > https://wg21.link/P1073 The problem: `constexpr` cannot guarantee the computation always happens at compile time ```cpp constexpr int f(int y); constexpr int x = f(100); // OK, guaranteed compile-time computation int y = 100; int z = f(y); // Not guaranteed ``` Now we can use `constval` to enforce compile-time computation. If it can't be done at compile time, an error will be raised. ```cpp consteval int sqrsqr(int n) { return sqr(sqr(n)); // Not a constant-expression at this point, } // but that's okay. ``` ## std::is_constant_evaluated > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0595r1.html ```cpp constexpr Regex compile_regex(string_view sv) { if (std::is_constant_evaluated()) return CompileTimeCompiledRegex(sv); else return RuntimeCompiledRegex(sv); // may require heap alloc } ``` ## Constexpr union > https://github.com/ldionne/wg21/blob/master/generated/p1330r0.pdf This proposal allows modifcation of active member in a union. ```cpp union Foo { int i; float f; }; constexpr int use() { Foo foo{}; foo.i = 3; foo.f = 1.2f; // valid now return 1; } static_assert(use()); ``` ## Constexpr try and catch > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1002r0.pdf Reason: must allow `try/catch` block in constexpr function (e.g., `std::vector::insert`), but current standard prohibits it. When evaluated in constant expression, an executed `throw` in `constexpr` function will raise a compile error. ```cpp constexpr int f(int x) { try { return x + 1; } // ERROR: can’t appear in constexpr function, Now permitted catch (...) { return 0; } } ``` ## constexpr dynamic_cast and typeid > [Link: Missing]: 404 Virtual function call in constant expression has already been allowed in the last meeting, now it comes to `dynamic_cast` and `typeid` ```cpp constexpr Derived a; constexpr Base* p = &a; constexpr int f(Base* p) { if (dynamic_cast<Derived*>(p)) { // ... } else { // ... } } constexpr result = f(p); ``` ## Constexpr std::pointer_traits > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1006r1.pdf ## Misc constexpr bits > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1032r1.html A bunch of small modifications: - `std::pair/tuple`: `piecewise_construct` constructor,`operator =`, `swap` - `std::array/array_view`: `swap` - `char_traits`: `move`, `copy`, `assign` - ` string_view`: `copy` - `insert_iterator` and many other iterators: `=`, `*`, `++` ## Revised Memory Model > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0668r4.html - Existing implementation schemes on Power and ARM are not correct with respect to the current memory model definition. These implementation schemes can lead to results that are disallowed by the current memory model when the user combines acquire/release ordering with seq_cst ordering. On some architectures, especially Power and Nvidia GPUs, it is expensive to repair the implementations to satisfy the existing memory model. Details are discussed in (Lahav et al) http://plv.mpi-sws.org/scfix/paper.pdf (which this discussion relies on heavily). The same issues were briefly outlined at the Kona SG1 meeting. We summarize below. - Our current definition of memory_order_seq_cst, especially for fences, is too weak. This was caused by historical assumptions that have since been disproved. - The current definition of release sequence is problematic, allowing seemingly irrelevant memory_order_relaxed operations to interfere with synchronizes-with relationships. - We still do not have an acceptable way to make our informal (since C++14) prohibition of out-of-thin-air results precise. The primary practical effect of that is that formal verification of C++ programs using relaxed atomics remains unfeasible. The above paper suggests a solution similar to http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3710.html . We continue to ignore the problem here, but try to stay out of the way of such a solution. [0] - The current definition of memory_order_consume is widely acknowledged to be unusable, and implementations generally treat it as memory_order_acquire. The current draft "temporarily discourages" it. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0371r1.html . There are proposals to repair it (cf. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0190r3.pdf and http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0462r1.pdf ), but nothing that is ready to go. [0]: Tl;dr for *out of thin air* result Assume x and y are both initialized as 0: ``` Thread 1: r1 = x.load(memory_order_relaxed); y.store(r1, memory_order_relaxed); Thread 2: r2 = y.load(memory_order_relaxed); x.store(r2, memory_order_relaxed); ``` This famously allows both r1 and r2 to have final values of 42, or any other "out of thin air" value. This occurs if each load sees the store in the other thread. It effectively models an execution in which the compiler speculates that both atomic variables will have a value of 42, speculatively stores the resulting values, and then performs the loads to confirm that the speculation was correct and nothing needs to be undone. No known implementations actually produce such results. However, it is extraordinarily hard to write specifications that present them without also preventing legitimate compiler and hardware optimizations. As a first indication of the complexity, note that the following variation of the preceding example should ideally allow x = y = 42, and some existing implementations can produce such a result: ``` Thread 1: r1 = x.load(memory_order_relaxed); y.store(r1, memory_order_relaxed); Thread 2: r2 = y.load(memory_order_relaxed); x.store(42, memory_order_relaxed); ``` ## Signed integers are two's complement > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1236r0.html > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r4.html - Status-quo Signed integer arithmetic remains non-commutative in general (though some implementations may guarantee that it is). - Change bool is represented as 0 for false and 1 for true. All other representations are undefined. - Change bool only has value bits, no padding bits. - Change Signed integers are two’s complement. - Change If there are M value bits in the signed type and N in the unsigned type, then M = N-1 (whereas C says M ≤ N). - etc (Read the paper for more) ## char8_t: A type for UTF-8 characters and strings > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0482r5.html The base type of `u8` char/string literals is changed to `char8_t`, which is basically the same as `unsigned char`, but does not alias with any other type. ```cpp char ca[] = u8"text"; // C++17: Ok. // This proposal: Ill-formed. char8_t c8a[] = "text"; // C++17: N/A (char8_t is not a type specifier). // This proposal: Ill-formed. ``` ## Nested Inline Namespaces > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1094r1.html ```cpp namespace std::experimental::inline parallelism_v2::execution { // ... } // equivalent to namespace std::experimental { inline namespace parallelism_v2 { namespace execution { // ... } } } ``` ## std::assume_aligned > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1007r2.pdf A unification of existing compiler-specific intrinsic: - GCC: `__builtin_assume_aligned(const void* ptr, size_t N)` - MSVC`__assume(expr)` - OpenMP: `#pragma omp simd aligned` for better vectorized code ```cpp void mult(float* x, int size, float factor) { float* ax = std::assume_aligned<64>(x); // we promise that x is aligned to 64 bytes for (int i = 0; i < size; ++i) // loop will be optimised accordingly ax[i] *= factor; } ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully