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>: reduce/scan algorithms aren't enforcing their Mandates #4573

Open
StephanTLavavej opened this issue Apr 10, 2024 · 2 comments
Open
Labels
bug Something isn't working

Comments

@StephanTLavavej
Copy link
Member

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",
@StephanTLavavej StephanTLavavej added the bug Something isn't working label Apr 10, 2024
@frederick-vs-ja
Copy link
Contributor

This seemingly supersedes #891. But some requirements seem bogus (#891 (comment)).

@StephanTLavavej
Copy link
Member Author

Thanks for the link! There was significant analysis in that issue, so let's keep both of them open for now.

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

No branches or pull requests

2 participants