microsoft / microsoft/STL

<numeric>: non conformance of std::reduce()

Open
#891 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Describe the bug
in std::reduce(), binary_op(*first,*first) must be convertible to T. This isn't the case in the below code yet it compiles.

Command-line test case

C:\Temp>type repro.cpp
#include <iostream>

int main() {
  std::random_device e;
        std::uniform_int_distribution<> dist(1, 10);
        const int n = 10;
        std::vector<int> v(n);
        std::generate(v.begin(), v.end(), [&]() {return dist(e); });  
        const auto result = std::reduce(v.begin(), v.end(), std::make_pair(0, 0),
            [](std::pair<int,int> sum,int n) {
                n % 2 == 1?sum.first += n:sum.second += n;
                return sum;
            });
}

C:\Temp>cl /EHsc /W4 /WX .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.26.28806 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

repro.cpp
Microsoft (R) Incremental Linker Version 14.26.28806.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:repo.exe
repro.obj

Expected behavior
It shouldn't compile

STL version
Microsoft Visual Studio Community 2019 Version 16.6.1

Additional context
gcc and clang reject the above code

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided repro.cpp and the std::reduce entry point; compare MSVC's acceptance with the stated gcc and clang behavior. Done means the invalid binary operation is rejected, with a regression test covering this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.