I have the typical reentrant C style parser, where the parsed data is contained in an union like the following one:
%union {
int number;
const char *string;
Item *item_ptr;
}
I would like to use Shared Pointers instead of normal pointers.
I cannot use std::shared_ptr
because I cannot compile the source code with C++11
, I am also forbidden to use boost::shared_ptr
. Thus, I have my own class SharedPtr
, implementing the desired behaviour.
Unfortunately, I cannot plug the SharedPtr
class within the union as follows:
%union {
int number;
const char *string;
SharedPtr<Item> item_ptr;
}
because I get the following error:
bisonparser.yy:92:20: error: member ‘SharedPtr<Item> YYSTYPE::item_ptr’ with constructor not allowed in union
bisonparser.yy:92:20: error: member ‘SharedPtr<Item> YYSTYPE::item_ptr’ with destructor not allowed in union
bisonparser.yy:92:20: error: member ‘SharedPtr<Item> YYSTYPE::item_ptr’ with copy assignment operator not allowed in union
bisonparser.yy:92:20: note: unrestricted unions only available with -std=c++11 or -std=gnu++11
An alternative could be inserting a level of indirection as follows:
%union {
int number;
const char *string;
SharedPtr<Item> *item_ptr;
}
However, I wonder if there is a much cleaner way to design my project so that I can use my SharedPtr
class directly instead of as a pointer. What are the minimal changes that I have too look for to get to the alternative solution?