-1

I have the following struct:

struct Feedback : public TaggedUnion<Feedback1Idx, String>
{
    using TaggedUnion<Feedback1Idx, String>::TaggedUnion;
    using TaggedUnion<Feedback1Idx, String>::operator=;

    bool isError = false;
};

... which inherits TaggedUnion's operator =, allowing me to write the following:

Feedback a = Feedback1Idx();
Feedback b = String();
Feedback c = Feedback();
Feedback d = b;

In the examples above, I would like a.isError to be true, b.isError to be true, but c.isError to be false (and d.isError to be true because b.isError is true). In other words, anytime an inherited operator = is used, I would like .isError to be switched to true.

How can I achieve this? (without having to add a constructor/assignment operator for each template parameter I add to TaggedUnion<>)

18
  • 3
    Note that none of the examples uses any overloaded assignment operator. They are all constructor calls. Commented Feb 16, 2023 at 12:46
  • You achieve this by defining an explicit assignment operator, instead of inheriting one? Commented Feb 16, 2023 at 12:53
  • @SamVarshavchik Indeed, however in the future I will add more template parameters to TaggedUnion<>, and so I would like this to be automatic if it is possible. I am not opposed to defining my own operator, this is more about "how do I define it in such a way that it wraps around the inherited operator?" as per the title; otherwise it would mean I would have to add an overload anytime I add a parameter to TaggedUnion<> Commented Feb 16, 2023 at 13:15
  • @Someprogrammerdude That is true, however the same problems applies, but with constructors instead of assignment operators. Ideally, I would define the same behavior for both of them. Commented Feb 16, 2023 at 13:19
  • So, have your user-defined operator call TaggedUnion::operator= explicitly? Commented Feb 16, 2023 at 13:27

1 Answer 1

0

I have found a simple solution:

struct Feedback : public TaggedUnion<Feedback1Idx, String>
{
    bool isError = false;

    Feedback() = default;

    Feedback(const Feedback& _other) = default;

    template<typename TYPE>
    Feedback(const TYPE& _other)
    : TaggedUnion<Feedback1Idx, String>(_other)
    {
        this->isError = true;
    }

    Feedback& operator=(const Feedback& _other) = default;
    
    template<typename TYPE>
    Feedback& operator=(const TYPE& _other)
    {
        this->isError = true;
        TaggedUnion<Feedback1Idx, String>::operator=(_other);
        return *this;
    }
};

A small thing to note: the compiler will probably scream at you if you attempt to do Feedback a = int, or some other type that is not in the inherited type.

Aside from that, should be all good (tested it a bit).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.