How to have a C++ stack with more than one data type?

Stack Tracer

Here's the problem:

I am currently trying to create a simple stack-based programming language (Reverse Polish Notation, FORTH style) as a component of a larger project. I have hit a snag, though.

There is no problem with creating a stack in C++ (by using std::vector<>) that would contain one type of element (I could use the syntax std::vector<double> Stack, for instance).

However, a programming language needs to be able to hold multiple data types, such as ints, doubles, strings, and 3D vectors (as in physics vectors with X, Y, and Z components), just to name some simple things.

So, is there a construct in C++ that I could use as a stack that would be able to store more than one kind of primitive type/object/struct?

Jon Purdy

Sure, one way is to use a tagged union:

enum Type { INTEGER, DOUBLE, /* ... */ };

union Data {
    uint64_t as_integer;
    double as_double;
    // ...
};

struct Value {
    Type type;
    Data data;
};

The storage for as_integer, as_double, etc. will be overlapped, so a Value structure will take up two words of storage, and your stack will have type std::vector<Value>. Then you access members of data according to the value of type:

void sub(std::vector<Value>& stack) {
    // In reality you would probably factor this pattern into a function.
    auto b = stack.back();
    stack.pop_back();
    assert(b.type == INTEGER);

    auto a = stack.back();
    stack.pop_back();
    assert(a.type == INTEGER);

    Value result;
    result.type = INTEGER;
    result.data.as_integer = a.data.as_integer - b.data.as_integer;
    stack.push_back(result);
}

Of course, Forths are usually untyped, meaning that the stack consists of words only (std::vector<uint64_t>) and the interpretation of a data value is up to the word operating on it. In that case, you would pun via a union or reinterpret_cast to the appropriate type in the implementation of each word:

void subDouble(std::vector<Data>& stack) {
    // Note that this has no type safety guarantees anymore.
    double b = stack.back().as_double;
    stack.pop_back();

    double a = stack.back().as_double;
    stack.pop_back();

    Data result;
    result.as_double = a - b;
    stack.push_back(result);
}

void subDouble(std::vector<uint64_t>& stack) {
    double b = reinterpret_cast<double&>(stack.back());
    stack.pop_back();

    double a = reinterpret_cast<double&>(stack.back());
    stack.pop_back();

    double result = a - b;
    stack.push_back(reinterpret_cast<uint64_t&>(result));
}

Alternatively, you can store not values but pointers to instances of a class Value from which other value types such as Integer or Double would derive:

struct Value {};
struct Integer : Value { uint64_t value; };
struct Double : Value { double value; };
// ...

Your stack would have type std::vector<unique_ptr<Value>> or std::vector<Value*>. Then you needn’t worry about different value sizes, at the cost of making wrapper structures and allocating instances of them at runtime.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to have a C++ stack with more than one data type?

From Dev

How to have more than one examples in Doxygen?

From Dev

How to replace more than one fragments with another one in the back stack?

From Dev

In Google App Engine for Go, how can a property have values of more than one type?

From Dev

Can an object have more than one effective type?

From Dev

Can I have more than one data-rule attribute?

From Dev

Is it possible to have more than one parameters in data for a FlatList?

From Dev

Data warehousing more than one data type in a fact table

From Dev

Is it possible to have more than one main() method in a C# program?

From Dev

How to have more than one controllers in AngularJS correctly?

From Dev

How To Have More Than One If statement in an input in JavaScript

From Dev

How can I have more than one click function in jQuery?

From Dev

How can a command have more than one output?

From Dev

How can I have more than one value for my php

From Dev

How to have xPath expression for more than one conditions

From Dev

How To Have More Than One If statement in an input in JavaScript

From Dev

How to know if I have more than one version of a library installed?

From Dev

How to have more than one instance of the same broadcast running simultaneously

From Dev

task list in C++ (vector with more than one type)

From Dev

How to display data from more than one table to my view using MVP pattern in C# winforms?

From Dev

How to return more than one type from a function?

From Dev

Guice: how to get more than one @Provides for a type?

From Dev

How to display more than one image in an <input type="text">

From Dev

How to create a record type more than one polymorphic variables

From Dev

Guice: how to get more than one @Provides for a type?

From Dev

How to display more than one image in an <input type="text">

From Dev

How to use more than one Component type (Embedded, Embeddable) in annotation

From Dev

Why do we need enum data type in C despite it uses more memory than implementing it by one variable string or even by #define

From Java

Is it possible to type hint more than one type?

Related Related

  1. 1

    How to have a C++ stack with more than one data type?

  2. 2

    How to have more than one examples in Doxygen?

  3. 3

    How to replace more than one fragments with another one in the back stack?

  4. 4

    In Google App Engine for Go, how can a property have values of more than one type?

  5. 5

    Can an object have more than one effective type?

  6. 6

    Can I have more than one data-rule attribute?

  7. 7

    Is it possible to have more than one parameters in data for a FlatList?

  8. 8

    Data warehousing more than one data type in a fact table

  9. 9

    Is it possible to have more than one main() method in a C# program?

  10. 10

    How to have more than one controllers in AngularJS correctly?

  11. 11

    How To Have More Than One If statement in an input in JavaScript

  12. 12

    How can I have more than one click function in jQuery?

  13. 13

    How can a command have more than one output?

  14. 14

    How can I have more than one value for my php

  15. 15

    How to have xPath expression for more than one conditions

  16. 16

    How To Have More Than One If statement in an input in JavaScript

  17. 17

    How to know if I have more than one version of a library installed?

  18. 18

    How to have more than one instance of the same broadcast running simultaneously

  19. 19

    task list in C++ (vector with more than one type)

  20. 20

    How to display data from more than one table to my view using MVP pattern in C# winforms?

  21. 21

    How to return more than one type from a function?

  22. 22

    Guice: how to get more than one @Provides for a type?

  23. 23

    How to display more than one image in an <input type="text">

  24. 24

    How to create a record type more than one polymorphic variables

  25. 25

    Guice: how to get more than one @Provides for a type?

  26. 26

    How to display more than one image in an <input type="text">

  27. 27

    How to use more than one Component type (Embedded, Embeddable) in annotation

  28. 28

    Why do we need enum data type in C despite it uses more memory than implementing it by one variable string or even by #define

  29. 29

    Is it possible to type hint more than one type?

HotTag

Archive