Re: how to avoid instantiation of template operator?
On Mar 27, 5:23 pm, "John Moeller" <fishc...@gmail.com> wrote:
On Mar 26, 5:19 am, "Michael Kilburn" <crusader.m...@gmail.com> wrote:
Ah. I think what you want is the double-conversion trick. No more
than one implicit conversion can be used in any one conversion
No more user-defined conversion... imho compiler could use any number
built-in conversions, and the are all implicit -- explicit conversions
are used only when you specifically ask for it... The rest is correct.
You would replace "t() == u()" with "ImplicitConvert<T>(t()) ==
ImplicitConvert<U>(u())". This would ensure that the implicit
conversion is done by a step that you control (ImplicitConvert<T>(t())
to T &, for example), instead of the match for the operator==().
yes, I need to exclude possible implicit conversions that could make
t() and u() comparable, while they could not be compared 'directly'.
- and the latest and most scary -- I'll need to find result type of
every operator. So, at the end every operator should look like:
You'll probably need to define this explicitly for many operators.
What if A() + B() results in an instance of C?
... [skip]
Since you're going through the effort of finding the return types of
all these operators, you could just specify it, instead of relying on
typeof, or maybe define a traits class for IsEqualComparable, or some
such. Additionally, you can usually count on (for example) operator==
taking const reference arguments, which will limit the specific cases
that you have to cover. It doesn't make sense for operator== to take
non-const arguments. The same kind of reasoning can be applied to
most operators. The case of T and U being the same class shouldn't
offer too much difficulty, but you could always define a
specialization that handled it if you had to.
My original idea was to implement accessor<T> such that any:
class A
{
T m_data;
public:
T const& data() const { return m_data; }
T& data() { return m_data; }
};
could be seamlessly replaced with
class A
{
T m_data;
public:
accessor<T const> data() const { return make_accessor(m_data); }
accessor<T> data() { return make_accessor(m_data); }
};
and this won't break any code regardless of type T. It was looking
simple first, then I ran into bewildering number of difficulties,
until I realised that:
- it could not be done without typeof (to detect return values for
oveloaded operators)
- it could be done only by overloading:
a) operators defined on T and only them (i.e. if T does not have
op==, accessor<T> should not have it too)
b) accessor<T> should be implicitly convertible to any type T is
convertible, but no more
Unfortuanately C++ does not have typeof, also it is very hostile to my
attempts to implement a) and b).
The next logical step was to augment accessor<> so that it could be
based not only on reference to T, but on some arbitrary functions that
return (get/set).
Bye.
Sincerely yours, Michael.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]