microsoft / microsoft/STL

`<numeric>`: reduce/scan algorithms aren't enforcing their Mandates

Open
#4,573 2 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

Reported on r/cpp: https://www.reddit.com/r/cpp/comments/1c0jld0/extended_use_of_stdreduce/

This is an accepts-invalid bug. None of these calls should compile, but we aren't enforcing the Standard's Mandates. For example, WG21-N4971 [reduce]/5 is:

Mandates: All of

  • binary_op(init, *first),
  • binary_op(*first, init),
  • binary_op(init, init), and
  • binary_op(*first, *first)

are convertible to T.

C:\Temp>type meow.cpp
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

void print_strings(const vector<string>& result) {
    for (const auto& elem : result) {
        cout << "\"" << elem << "\", ";
    }
    cout << endl;
}

int main() {
    const vector<int> v{1, 2, 3, 4, 5};
    auto string_plus_int = [](const string& a, int b) { return a + to_string(b); };
    auto square          = [](int x) { return x * x; };

    { // N4971 [reduce]/5
        const auto str = reduce(v.begin(), v.end(), string{}, string_plus_int);
        cout << str << endl;
    }

    { // N4971 [transform.reduce]/3
        const auto str = transform_reduce(v.begin(), v.end(), v.begin(), string{}, string_plus_int, plus<int>{});
        cout << str << endl;
    }

    { // N4971 [transform.reduce]/7
        const auto str = transform_reduce(v.begin(), v.end(), string{}, string_plus_int, square);
        cout << str << endl;
    }

    { // N4971 [exclusive.scan]/3
        vector<string> result;
        (void) exclusive_scan(v.begin(), v.end(), back_inserter(result), string{}, string_plus_int);
        print_strings(result);
    }

    { // N4971 [inclusive.scan]/4
        vector<string> result;
        (void) inclusive_scan(v.begin(), v.end(), back_inserter(result), string_plus_int, string{});
        print_strings(result);
    }

    { // N4971 [transform.exclusive.scan]/1
        vector<string> result;
        (void) transform_exclusive_scan(v.begin(), v.end(), back_inserter(result), string{}, string_plus_int, square);
        print_strings(result);
    }

    { // N4971 [transform.inclusive.scan]/2
        vector<string> result;
        (void) transform_inclusive_scan(v.begin(), v.end(), back_inserter(result), string_plus_int, square, string{});
        print_strings(result);
    }
}
C:\Temp>cl /EHsc /nologo /W4 /std:c++17 /MTd /Od meow.cpp && meow
meow.cpp
12345
246810
1491625
"", "1", "12", "123", "1234",
"1", "12", "123", "1234", "12345",
"", "1", "14", "149", "14916",
"1", "14", "149", "14916", "1491625",

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

The issue names no repository files or tests; start by locating the numeric algorithm entry points for reduce, transform_reduce, and the four scan variants. Use the provided meow.cpp reproducer and the cited WG21 mandates as the first checks. Done means each listed invalid call is rejected according to its cited mandate, with tests covering the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.