2

I can compile the following code (using std::transform_reduce) with gcc 9.2.1 on both Fedora and Ubuntu, but attempting to compile on clang see godbolt fails, and I've got a report that some FSF version of gcc 9.2.1 also refuses to compile the code, requiring a std::execution_policy as the first argument to the std::transform_reduce.

#include <vector>
#include <algorithm>
#include <numeric>

auto brokenvector(std::vector<int> const& a, std::vector<int> const& b) 
{
  return std::transform_reduce(cbegin(a), cend(a), cbegin(b), 0, std::plus<>{},std::multiplies<>{});
}

I specifically cannot use a std::execution_policy here, and both cppreference and the C++ draft standard document n4659 show overloads without an execution policy.

Have I stepped into some kind of political minefield where half of the available compilers refuse to implement the standard, or is the code incorrect?

EOF
  • 6,273
  • 2
  • 26
  • 50
  • 1
    C++17 is (relatively) new. I wouldn't be surprised if the answer is simply: Some library implementers just didn't implement this yet – Not a real meerkat Jan 30 '20 at 22:41
  • So it is portable as long as compilers/libraries respect (implement fully) the standard. – Jarod42 Jan 30 '20 at 22:55
  • [this question and its answers may be interesting for you](https://stackoverflow.com/questions/42567998/how-do-i-use-the-new-c17-execution-policies/42587914#comment86495939_42587914) – Not a real meerkat Jan 30 '20 at 23:44
  • @CássioRenan That's kind of the opposite of my problem, I explicitly do *not* want to specify an execution policy. Furthermore, while the answer "C++17 is not yet fully supported" was entirely reasonable in 2017, it's a bit less understandable in 2020. My understanding of the C++ evolution was that the goal is to have most new features already implemented in multiple compilers before standardizing them, and to me as a mostly-C-developer std::algorithm seems like the crown jewel of recent c++ standards. This is baffling to me. – EOF Jan 31 '20 at 01:22
  • @EOF I know. I thought you would think it was interesting because it exemplifies your issue: That standard features may take a (sometimes long) while to actually be implemented (If you look at one of the comments there, MSVC took over 6 years to finish support of C++11!) – Not a real meerkat Jan 31 '20 at 02:16

1 Answers1

5

This is a libstdc++ vs libc++ issue. libc++ implements the function and you can see it working with clang on godbolt using -stdlib=libc++ in this live example. gcc implements it now in trunk, but the currently released versions do not. The function was added in this commit.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • @walnut Thanks for that. Added to the answer. – NathanOliver Jan 30 '20 at 22:57
  • Thanks a lot! Interesting that both Ubuntu 19.10 and Fedora 31 apparently have libstdc++-versions that already include this function. Either way, this answer should be very useful for me. – EOF Jan 30 '20 at 23:07