Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<numeric>: Parallel transform_reduce incorrectly uses list-initialization #4129

Closed
frederick-vs-ja opened this issue Oct 26, 2023 · 0 comments · Fixed by #4260
Closed

<numeric>: Parallel transform_reduce incorrectly uses list-initialization #4129

frederick-vs-ja opened this issue Oct 26, 2023 · 0 comments · Fixed by #4260
Labels
bug Something isn't working fixed Something works now, yay!

Comments

@frederick-vs-ja
Copy link
Contributor

Describe the bug

The following program doesn't compile with MSVC STL due to narrowing conversion in list-initialization.

(originally discovered by @Mq-b in Mq-b/Loser-HomeWork@099c66a)

#include <execution>
#include <numeric>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string>strVec{"Only", "for", "testing", "purpose"};
    std::size_t res = std::transform_reduce(
        std::execution::par,
        strVec.begin(), strVec.end(),
        0,
        [](std::size_t a, std::size_t b) {return a + b; },
        [](std::string s) {return s.size(); }
    ); // the value of res is 21
}

_Ty _Val{_Reduce_op(_Transform_op(*_Chunk._First), _Transform_op(*++_Next))};

Per [transform.reduce], IIUC list-initialization shouldn't be used here. We should only use implicit conversion (in other words, copy-initialization).

There're some other parallel numeric algorithms direct-initializing _Ty variables from the results of the binary operations. Perhaps they are also buggy.
(Despite being contrived, it's possible to make T(bin_op(l, r)) ill-formed while T t = bin_op(l, r); being well-formed.)

Command-line test case

Godbolt link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Something works now, yay!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants