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

<experimental/generator>: Problem using generator for simple test case "cannot create a pointer to reference" #4999

Closed
ValorZard opened this issue Oct 2, 2024 · 2 comments
Labels
wontfix This will not be worked on

Comments

@ValorZard
Copy link

ValorZard commented Oct 2, 2024

Describe the bug

I tried porting the example on how to use std::generator from cppreference
and i get this error:

Severity	Code	Description	Project	File	Line	Suppression State	Details
Error	C2528	'abstract declarator': you cannot create a pointer to a reference	test_generators	C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\include\experimental\generator	109

Command-line test case

#include <experimental/generator>
#include <iostream>

template<typename T>
struct Tree
{
    T value;
    Tree* left{}, * right{};

    std::experimental::generator<const T&> traverse_inorder() const
    {
        if (left)
            for (const T& x : left->traverse_inorder())
                co_yield x;

        co_yield value;
        if (right)
            for (const T& x : right->traverse_inorder())
                co_yield x;
    }
};

int main()
{
    Tree<char> tree[]
    {
                                    {'D', tree + 1, tree + 2},
                                    //
                                    //            ┌───────────────┴────────────────┐
                                    //            │                                │
                                                {'B', tree + 3, tree + 4},       {'F', tree + 5, tree + 6},
                                                //            │                                │
                                                //  ┌─────────┴─────────────┐      ┌───────────┴─────────────┐
                                                //  │                       │      │                         │
                                                  {'A'},                  {'C'}, {'E'},                    {'G'}
    };

    for (char x : tree->traverse_inorder())
        std::cout << x << ' ';
    std::cout << '\n';
}

Expected behavior

Should work and print out A B C D E F G

STL version

image

Additional context

Just in case this is helpful:
image

@Veeloxfire
Copy link

Note this is likely because the generator iterator current assumes _Ty to be a value type in order to fulfill a better iterator category

struct iterator {
    using iterator_category = input_iterator_tag;
    using difference_type   = ptrdiff_t;
    using value_type        = _Ty;
    using reference         = const _Ty&;
    using pointer           = const _Ty*;

the libstdc++ iterator uses a less specified iterator and to not have to make this assumption

using _Value = __conditional_t<is_void_v<_Val>,
                              remove_cvref_t<_Ref>,
                              _Val>;
template<class _Ref, class _Val, class _Alloc>
struct generator<_Ref, _Val, _Alloc>::_Iterator
{
  using value_type = _Value;
  using difference_type = ptrdiff_t;

@CaseyCarter CaseyCarter changed the title <experimental/generator>: Problem using std::generator for simple test case, cannot create a pointer to reference <experimental/generator>: Problem using generator for simple test case "cannot create a pointer to reference" Oct 2, 2024
@StephanTLavavej StephanTLavavej added the wontfix This will not be worked on label Oct 2, 2024
@StephanTLavavej
Copy link
Member

Thanks for reporting this issue. We talked about it at the weekly maintainer meeting and we're going to resolve this as wontfix This will not be worked on as we've merged Standard <generator> (@CaseyCarter's #4953) for VS 2022 17.13 Preview 1. Instead, we'll look into deprecating and removing <experimental/generator>, as we don't want to support experimental machinery forever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants