Re: template argument deduction oddity?
On 19 Dez., 21:13, news.nospam.cech...@gmail.com wrote:
This is not a big complicated generic thing, but the compiler is
having some trouble.
template <class D>
struct GenericThing
{
struct T { };
};
struct Stream
{ };
template <class D>
Stream& operator<< (
Stream& out,
typename GenericThing<D>::T const & thing)
{
return out;
}
Indeed, the compiler cannot resolve D in this
situation (which is a so-called nondeduced
context). The reason for this is, because C++
templates are a priori unconstrained and the
compiler cannot not safely guarantee that it
can deduce D here. Consider the following
specializations for GenericThing:
template <>
struct GenericThing<int>{}; // Ooops, no T
or this one
template <typename T>
struct GenericThing<T*> {
int T; // Oops, T is not a type
};
So what rule of C++ is preventing this deduction? I presume it has
something to do with the argument type being *inside* the template
class as opposed to *being* the templated type.
Have a look at [temp.deduct.type]/5+6 in the recent
draft N2461 or [temp.deduct.type]/4 in the official
standard 14882-2003, which explains the concept of
non-deduced context during template argument deduction.
And how would one get around this problem?
That depends. The most simple workaround
given your snippet is simply to write:
template <class D>
Stream& operator<< (
Stream& out,
D const & thing)
{
return out;
}
You need to provide more details of what you
want to realize to give you better advice.
HTH & 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! ]