The example below illustrates a more complex but not dissimilar problem I’ve been trying to solve elegantly. I have a set of templates which must be specialized and, in doing so, implement one or both of two interfaces: Readable and Writable, in each specialization. Specific implements both interfaces, and is then tested using main:
class Readable
{
protected:
int values[3];
public:
Readable()
{
// Does nothing.
}
int operator()(int i) const
{
return values[i];
}
};
class Writable : public Readable
{
public:
Writable()
{
// Does nothing.
}
using Readable::operator ();
int& operator()(int i)
{
return values[i];
}
};
class Specific : public Writable
{
};
void write_test(Specific& specific)
{
// Error C2106: '=' : left operand must be l-value
specific(0) = 1;
}
int main()
{
Specific s;
write_test(s);
return 0;
}
The code above fails on VS 2008, 2010 with the following:
error C2106: ‘=’ : left operand must be l-value.
This strikes me as odd: have I overlooked something simple? I’ve compiled and run exactly this code using the [] operator, and all was well (as it should and always has been). It would appear to be some issue relating specifically to the behavior of this operator, an issue I am unfamiliar with.
This is a compiler error: the using declaration should work. To work around the problem just use delegation:
This implementation is longer than the implementation actually delegated to but it avoids problems if the version in
Readableever changes.