Skip to content

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

Description

Describe the bug

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

(originally discovered by mq白 (@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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions