weird typedef explanation

Hi, I'm reading one C++ book and in one example there was this very weird typedef that I have no idea how to use and what exactly does it mean. It was not the important part of the example so its not mentioned anymore.

So here it is
 
typedef enum {red, green, blue} *ptrColor;


So I'm looking at it and I have no idea how this ptrColor could actually be used somewhere. That enum is unnamed so how exactly am I supposed to create any instance of that enum if all I have is pointer to that enum and nothing more?

1
2
3
4
int main()
{
    ptrColor my_ptr= /* . . . */;
}
*ptrColor is a typedef of type enum, so something like ptrColor->blue comes to mind.
That gives me
Error	C2227	left of '->red' must point to class/struct/union/generic type
I believe that an enum like that will export its names to the surrounding scope, so you can just use "red" directly. That said, I don't know how you could instantiate such an object without being able to name it.

Perhaps you could cheat with something like auto aColor = red;?
Yea good one, that works :D Tnx a lot man. The book I saw it in was actually C++ Templates The Complete Guide and of course its a very old book. I wonder how it could be used back then without auto.

You kind of answered my question but I'll leave this unanswered for a moment just to see if someone could tell something about any real use of this interesting typdef :)
In the book, typedef enum { red, green, blue } *ColorPtr; is used merely to illustrate a restriction that was there in legacy C++.

2. Types that involve unnamed class types or unnamed enumeration types cannot be template type arguments ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <typename T> class List {
…
};

// ... 

typedef enum { red, green, blue } *ColorPtr;

int main()
{
    // ....

    List<ColorPtr> error2; // ERROR: unnamed type in template argument
}


The other restriction was:
1. Local classes and enumerations (in other words, types declared in a function definition) cannot be involved in template type arguments.


Both these restrictions were removed in C++11.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <utility>

template < typename T > struct List { /* ... */ };

int main()
{

    typedef enum {red, green, blue} *ptrColor;

    List<ptrColor> a ;

    List< decltype( *std::declval<ptrColor>() ) > b ;
    
}

http://coliru.stacked-crooked.com/a/07e108357211c7ab
http://rextester.com/VDLY16571
Tnx man for making this clear. I actually thought that besides making this point (point 2) this enum pointer was actually useful in some situations. I was wrong but thanks very much man! :)
Topic archived. No new replies allowed.