Re: Frustrated in autocast failure
On 5 Mrz., 19:29, adrian.hawry...@gmail.com wrote:
adrian.hawry...@gmail.com wrote:
Perhaps this is caused by the fact that string is a template class?
Well, if it is caused by it being a template class, it would have to
be because it is calling a dependent template function (i.e. in the
function the first param template type takes C and T and the second
template type takes C, T, and A) and the compiler is getting
confused. I've tried the following template test with out a problem:
I had already written an answer, which never came through (*not*
my reply to Ben), but which tried to explain the problem. As
you have correctly recognized, your problem is *not* related to
the fact, that *your* class is a template, but instead it is
related to the fact, that the actual called inserter of the
wrapped type (in this case: std::string) is a function template,
more precisely its declared as
template<class charT, class traits, class Allocator>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os ,
const basic_string<charT,traits,Allocator>& str);
According to the deduction rules of function templates
described in [temp.deduct.call], there will be *no* attempt
to try out user-defined conversions (which is what you
expected), only some very basic deviations between each
template parameter and its corresponding argument (which
is Proxy<std::string> or deref<std::string>, resp.) are
accepted, these are (in this special case):
- stronger cv-qualifications than the actual type.
- the given type has a derived-base-class relation to
the template parameter.
Both of these are not sufficient in your case to come
to a successful deduction.
The proper resolution is by simply providing your own
inserter, which simply delegates to that of the wrapped
type:
template <class T, class Ch, class Tr>
inline basic_ostream<Ch, Tr>&
operator<<(basic_ostream<Ch, Tr>& os, deref<T> const &rhs)
{
return os << *rhs.pObj;
}
With this addition, your original program will compile,
whether you used templates or not.
Your "solution" below is no real solution, it compiles only
because you have uncommented the code, which will not compile.
I assume that a reason for this is probably your hard-to-read
style. In this situation a single #define and a proper
#ifdef logic would have been more readable than the below
written stuff with lots of code-commenting:
// dummy A class for 'allocating' (does nothing)
template <class T, class A>
struct Proxy {
T* pObj;
static A alloc;
Proxy(T * pObj) : pObj(pObj) {}};
template <class T, class A>
A Proxy<T,A>::alloc;
Actually I don't understand, for which reasons you
introduced this Proxy class (including the static member,
which seems to have no influence on this program).
I propose to provide any example code to be as short
and concise as possible.
#define T deref
//template<class T>
ostream& fn(ostream& os, T const & rhs)
{
//os << rhs;
No wonder that this compiles now... ;-)
Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]