cppcoro icon indicating copy to clipboard operation
cppcoro copied to clipboard

Test failing with G++, succeeding with clang++

Open eterchun opened this issue 3 years ago • 0 comments

When testing the following code, I am able to compile and execute when using clang++.

#include <cppcoro/generator.hpp>
#include <cppcoro/recursive_generator.hpp>
#include <iostream>
#include <type_traits>

cppcoro::generator<int> numbers(int begin, int end)
{
    for (int i = begin; i < end; ++i)
    {
        co_yield i;
    }
}

int main(int argc, char* argv[])
{
    int begin = 10;
    int end = 30;
    auto predicate = [](auto&& obj) -> bool { return true; };
    auto filter = [&pred_ = predicate](auto&& val) -> cppcoro::generator<typename std::decay_t<decltype(val)>::iterator::value_type> {
        for (auto&& _v : val) {
            if (pred_(_v)) {
                co_yield _v;
            }
        }
      };
    auto range = filter(numbers(begin, end));
    for (auto i : range) {
        std::cout << i << "\n";
    }
}

However, g++ gives the following error:

clang-test.cpp: In instantiation of ‘main(int, char**)::<lambda(auto:12&&)> [with auto:12 = cppcoro::generator<int>; typename std::decay<decltype (val)>::type::iterator::value_type = int]’:
clang-test.cpp:26:44:   required from here
clang-test.cpp:22:17: error: ‘co_yield’ cannot be used in a ‘constexpr’ function
   22 |                 co_yield _v;
      |                 ^~~~~~~~

Compilation with g++ succeeds if I remove the if statement in filter.

I am using gcc version 10.2.0 and clang version 10.0.1. I have not found an explanation for why this behavior would differ between platforms, but a solution under GCC would be appreciated.

eterchun avatar Oct 31 '20 23:10 eterchun