Skip to content

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

Description

@ValorZard

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions