30

Suppose, for example, you want to implement a spreadsheet Cell in C++. A cell can be either a string, a number, or perhaps empty. Ignore other cases, like it being a formula.

In Haskell, you might do something like:

data Cell = CellStr String | CellDbl Double | None

What is considered the current "best practice" for doing it in C++? Use a union in a structure with a type indicator, or something else?

9
  • 10
    One possible option is boost::variant. Commented Mar 29, 2016 at 11:55
  • or implement yourself specific variant type with union
    – David Haim
    Commented Mar 29, 2016 at 12:04
  • 3
    @Pixelchemist make that an answer and I'll +1 it. Commented Mar 29, 2016 at 12:22
  • 1
    I would go with a sorted vector<pair<XYCoords, double>> doubles; and a sorted vector<pair<XYCoords, string>> strings;. For a given cell coordinate you lower_bound into the doubles, if you didn't find it you do the same for the strings, otherwise it is None. Drawing the screen should be very fast, you just iterate through the vectors. Calculations are a bit messy, because they depend on the type, but you can probably abstract that away. Effectively I just cheated and never combined different types into one. Anyway, the question is too broad and opinionated.
    – nwp
    Commented Mar 29, 2016 at 12:26
  • 1
    @MvG Unfortunately, the highlighting code for haskell is lang-hs instead of lang-haskell. Keep this in mind the next time you want to add highlighting of Haskell code.
    – Bakuriu
    Commented Mar 29, 2016 at 15:07

2 Answers 2

24
struct empty_type {};
using cell_type = boost::variant<std::string, double, empty_type>;

Then you would do something with the cell with:

boost::apply_visitor(some_visitor(), cell);
16
  • 2
    Also note that there is a proposal for standardising std::variant (original proposal here)
    – filipos
    Commented Mar 29, 2016 at 12:30
  • @filipos in my view the proposal is flawed since it seeks to mandate allowing a variant to be empty. I sincerely hope it's rejected in favour of one that models the boost variant more closely. Commented Mar 29, 2016 at 12:33
  • 2
    The latest version does not mandate that. To use an empty state, you explicitly add the type monostate to the type list. It is true though that a variant can become invalid (not empty) under exceptional conditions.
    – filipos
    Commented Mar 29, 2016 at 12:41
  • 3
    @filipos it seems to me that they're mixing concerns. optional is one concern, variant is another. If the proposer wants an optional variant, he can use optional<variant<...>>. A variant should never be allowed to be invalid, even after a move - it should simply contain a moved-from T. Commented Mar 29, 2016 at 12:44
  • 4
    "LEWG opted against introducing an explicit additional variant state, representing its invalid (and possibly empty, default constructed) state."
    – filipos
    Commented Mar 29, 2016 at 12:46
5

Inheritance?

I have to say that I do not really like this method and would not consider it modern, but it still seems to be standard.

class DoubleCell : public Cell {
    double value;

    public:
    DoubleCell( double v ) : value(v) {}
    double DoubleValue() { return value; }
    ...
};

class StringCell : public Cell {
    std::string value;

    public:
    StringCell( std::string v ) : value(v) {}
    std::string StringValue() { return value; }
    ...
};

class EmptyCell : public Cell {
    ...
};

Some of the drawbacks are:

  • When getting the actual value, you need to use different functions. This will usually involve using instanceof and casting.

  • Different objects cannot directly be put into a container, only as pointers.

13
  • 1
    This only partly answers the question. How would you get a value from such a cell ??? getValue(){return value;} Commented Mar 29, 2016 at 11:58
  • 2
    Pointer semantics, dynamic memory allocations and virtual function calls for every single cell doesn't seem like a good idea to me.
    – nwp
    Commented Mar 29, 2016 at 12:01
  • 2
    Templates won't really work (at least when implemented like in the example) because the type of each cell would need to be known at compile-time. I guess you can combine both approaches by making the template derived from a common base class, but it would have the same performance overhead as the first method then.
    – interjay
    Commented Mar 29, 2016 at 12:15
  • 2
    @FrankPuffer The use of the library is just so they do not have to roll their own. In your example how would you get the value from the cell? Until you get that this really is only half an answer. Commented Mar 29, 2016 at 12:39
  • 3
    I don't think the answer should be deleted. It is a sensible design solution, and is worthy of discussion, even if it is only to say that there are better solutions.
    – blippy
    Commented Mar 29, 2016 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.