In this example code, why isn't
using IParameterBase<TYPE>::operator=;
working, and the base class assignment operator working?
I recently changed to this templated version, previously I had written individual type classes where this mechanism worked.
#include <cstdint>
#include <cstddef>
class IParameter
{
public:
explicit IParameter(const size_t size) : size_{size} {};
virtual ~IParameter() = default;
virtual void copy(uint8_t*& addr) = 0;
private:
const size_t size_;
};
template <class TYPE>
class IParameterBase : public IParameter
{
public:
explicit IParameterBase(const TYPE value) : IParameter{sizeof(TYPE)}, value_{value} {};
~IParameterBase() = default;
virtual void update(const TYPE value) = 0;
operator auto() const {return get();};
TYPE operator= (const TYPE value) { update(value); return get(); };
TYPE get() const {return value_;};
protected:
TYPE value_;
};
template <class TYPE>
class ParameterTx : public IParameterBase<TYPE>
{
public:
explicit ParameterTx(const TYPE value) : IParameterBase<TYPE>{value} {};
using IParameterBase<TYPE>::operator=;
void copy(uint8_t*& addr) override
{
/* copy stuff */
}
void update(const TYPE value) override
{
this->value_ = value;
}
};
int main ()
{
ParameterTx<uint16_t> param1{0};
ParameterTx<uint16_t> param2{1};
param1 = 16;
param2 = 5;
param1 = param2;
}
Code here: https://godbolt.org/z/3vqd4ebYM
I expect the assignment at the bottom param1 = param2;
to resolve to uint16_t, instead it's trying to copy the object, which is not what I want.
IParameter
has a const member, which makes it unassignable. – Portunausing IParameterBase<TYPE>::operator=
version. – Nurslingparam1 = param2
you're passingparam2
which is of typeParameterTx<uint16_t>
so only the deleted copy assignment operator= is a candidate because the one bought in byusing IParameterBase<TYPE>::operator=;
has a parameter of typeconst uint16_t value
– Nurslingparam1 = param2;
should be equivalent toparam1 = static_cast<uint16_t>(param2);
? It isn't, and implicit conversions are Evil. (Also, un-templating this code has the same problem.) – Portuna