Topic: Serious bug in CD2 templates?


Author: bparker@gil.com.au (Brian Parker)
Date: 1997/05/02
Raw View

Hi all,

I've posted this query a couple of times in the past without receiving
any comments, but it seems like such a serious issue to me that I
thought I would raise it again.

Using deferred-evaluation or proxy classes is an important idiom in
C++ and likely to become more so as more use is made of templates; one
example is using a proxy class to distinguish lvalue and rvalue usage
of an indexing operator, another is deferring evaluation of an
expression so it can be evaluated in an optimised fashion, as in my
template composite operators library, or the expression templates
technique, and there are probably numerous other examples.

These schemes rely upon a proxy conversion operator being called to
evaluate the proxy when it is used in a function call. However, it is
not clear to me that the proxy will be evaluated when calling
functions involving a template type, by the CD2 rules.

For examples,

class DeferredEvaluationClass {
public:

 operator complex<double> ( ) { ...}
};

template<typename T>
complex<T> conj(const complex<T>& z) { ... }

Will operator complex<double> be called to evaluate the proxy when
passing a DeferredEvaluationClass to conj( ) ?
Section 14.8.2 [temp.deduct] paragraph 10 states that
"Conversions (4) will be performed on a function argument that
corresponds with a function parameter that contains only non-deducible
template parameters..."

I managed to convince myself that this allowed the user-defined
conversion of the proxy to be called as T was otherwise non-deducible,
but I am now not so sure- I think that this a genuine bug in the
standard (though what exactly the afore-mentioned paragraph means I
don't know).

(Note that in the case of complex<double>, one can define non-template
forwarding functions for all of the relevant library global functions,
but of course this is not possible in the general case)

Can anyone confirm what the draft's intent is in this area? Will the
proxy be evaluated in the above example? What is the intent of the
quoted sentence?

If the draft does not evaluate the proxy in the above example, then I
think it should be fixed to do so. It would simply need an addition to
[temp.deduct] paragraph 4.

"-If P is a class, and P has the form class-template-name<arguments>,
A can be a derived class of the deduced A, or A can be convertible via
a user-defined conversion to the deduced A. ..."
"These alternatives are considered only if type deduction can not be
done otherwise."

I don't see how allowing a user-defined conversion in this case would
be any more onerous than the derived-to-base conversion already
allowed.

Unfortunately, I have appeared to have missed the various public
comment periods- I only submitted the weaker comment that paragraph 10
was unclear.

Has this issue been discussed previously within the committee?

Any comments?

Thanks for your time,
Brian Parker (bparker@gil.com.au)

---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]