Diamond Problem

Sven van Huessen | Nov 4, 2025

Diamond “Problem” and Virtual Inheritance

As explained in my previous blog post, I used mixins for a cleaner and more explicit widget/settings structure. But as you might’ve noticed, the mixins use virtual inheritance:

struct radio_mixin : virtual input_base {
    as_radio() {set_input(input_type::Radio); }
};

The reason for this is, without it, it would run into the Diamond Problem.

Let’s say we have this code, same as before (simplified) but without the virtuals:

/* Base */
struct input_base {
    input_type type;
    void set_input(const input_type v) { type = v; }
    input_type_widget get_input_type() { return type; }
}

/* Mixins */
struct radio_mixin : input_base {
    as_radio() {set_input(input_type::Radio); }
};

struct checkbox_mixin : input_base {
    as_checkbox() {set_input(input_type::Checkbox); }
};

struct bool_settings : radio_mixin, checkbox_mixin {
};

bool_settings inherits from both radio_mixin and checkbox_mixin, which both inherit from input_base. This means, because both radio and checkbox inherit from input_base, bool settings technically has 2x input_base.

Diamond Problem

So if you would call bool_settings::set_input(...) or bool_settings::get_input_type() it would throw an ambiguous error because it doesn’t know which one it should call.

To solve this, you would have to make both mixins virtually inherit from input_base. This “combines” the 2 input_base classes together, and gives you:

struct radio_mixin : virtual input_base {
    as_radio() {set_input(input_type::Radio); }
};

struct checkbox_mixin : virtual input_base {
    as_checkbox() {set_input(input_type::Checkbox); }
};

Diamond Problem Fixed

This Diamond “problem” helped me make my code a lot cleaner. No matter which mixin the type setting would inherit from, they all change the same type property.