Topic: Template argument deduction


Author: wmm@fastdial.net
Date: 1999/05/27
Raw View
In article <jayz-2505990940010001@205.178.30.6>,
  jayz@best.no.spam.com (Jay) wrote:
> I was surprised to find that the second line of main below causes a
> compilation error on the compiler I use. From reading "14.8.2.1
Deducing
> template arguments from a function call" and "14.8.2.4 - Deducing
template
> arguments from a type". It is not clear to me if this is or is not
correct
> behavior. Would anyone care to provide an analysis as to whether or
not
> this should compile?
>
>   ---------------
>   int  i;
>   int* pi;
>   volatile int  vi;
>   volatile int* pvi;
>
>   template <class T>  void f(T, T*);
>   template <class T>  void g(T*, T);
>
>   int main()
>       {
>       f( i,  pi);    // OK
>       f(vi, pvi);    // Not OK?
>       g(pvi, vi);    // OK
>       }

Both the second call to f and the call to g are incorrect.  The
reason is that top-level cv-qualifiers (in this case, volatile)
in the argument type are ignored in deducing the parameter type.
(See 8.3.5p3 -- "f(int);" and "f(const int);" declare the same
function.  This is reflected in 14.8.2.1p2.)  Since you can't
get a "volatile int" parameter declaration, and you can't pass a
"volatile int*" to an "int*", there's no way for the deduction
to succeed.

--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)


--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1999/05/27
Raw View
jayz@best.no.spam.com (Jay) writes:

> I was surprised to find that the second line of main below causes a
> compilation error on the compiler I use. From reading "14.8.2.1 Deducing
> template arguments from a function call" and "14.8.2.4 - Deducing templat=
e
> arguments from a type". It is not clear to me if this is or is not correc=
t
> behavior. Would anyone care to provide an analysis as to whether or not
> this should compile?

It should not compile. In particular, both the second call to f, and
the call to g are ill-formed. Let's take the letter:

  template <class T>  void g(T*, T);
      g(pvi, vi);    // OK - no

14.8.2.1/1 tells use that we must determine the types P and A. P is
(T*, T). For A, it would seem we get (volatile int*,volatile int),
but 14.8.2.1/2 says

>> If A is a cv=ADqualified type, the top level cv=ADqualifiers of A's
>> type are ignored for type deduction.

Ignoring top-level cv-qualifiers, we get (volatile int*, int). Now,
according to 14.8.2.4/2, we determine template parameters for each
pair of P/A. For the first pair, we get

T =3D volatile int

and for the second, we get

T =3D int

Now, 14.8.2.4/2 says

>> If ... different pairs yield different deduced values, ...
>> template argument deduction fails.

which is what should happen: template argument deduction fails, and
the template is not a candidate for overload resolution. Overload
resolution then finds that there is no viable candidate, and the call
is ill-formed.

Hope this helps,
Martin
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jayz@best.no.spam.com (Jay)
Date: 1999/05/26
Raw View
I was surprised to find that the second line of main below causes a
compilation error on the compiler I use. From reading "14.8.2.1 Deducing
template arguments from a function call" and "14.8.2.4 - Deducing template
arguments from a type". It is not clear to me if this is or is not correct
behavior. Would anyone care to provide an analysis as to whether or not
this should compile?

  ---------------
  int  i;
  int* pi;
  volatile int  vi;
  volatile int* pvi;

  template <class T>  void f(T, T*);
  template <class T>  void g(T*, T);

  int main()
      {
      f( i,  pi);    // OK
      f(vi, pvi);    // Not OK?
      g(pvi, vi);    // OK
      }
  ---------------

-----------------------------------------------------
(Remove the .no.spam from my e-mail address to reply)
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/04/18
Raw View
>>>>> "BF" == Brian Michael Freyburger <freyburg@glitch.stanford.edu> writes:
BF> Reading section 14.10.2 of the January WP, I can not figure out
BF> whether the following function call can be deduced:

BF> template <class S, class T = S>
BF> int foo(S s, T t = T());

It's a non-issue since default template arguments cannot be
specified for function templates.

 Daveed
---
[ 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                             ]





Author: freyburg@wavelet.stanford.edu (Brian Michael Freyburger)
Date: 1996/04/18
Raw View
> >>>>> "BF" == Brian Michael Freyburger <freyburg@glitch.stanford.edu> writes:
> BF> Reading section 14.10.2 of the January WP, I can not figure out
> BF> whether the following function call can be deduced:
>
> BF> template <class S, class T = S>
> BF> int foo(S s, T t = T());
>
> It's a non-issue since default template arguments cannot be
> specified for function templates.
>
>  Daveed

Where in the working papers does it say this?  They appear to me to be
introduced in 14.8 [temp.arg], which as far as I can tell applies to
both functions and classes.  I see nothing in 14.10 [temp.fct] which
disallows them--what did I miss?

Why are they disallowed?  Are there any technical difficults?

Thanks,

Brian


[ 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                             ]





Author: austern@isolde.mti.sgi.com (Matt Austern)
Date: 1996/04/19
Raw View
In article <FREYBURG.96Apr18141053@wavelet.stanford.edu>
freyburg@wavelet.stanford.edu (Brian Michael Freyburger) writes:

> > It's a non-issue since default template arguments cannot be
> > specified for function templates.
> >
> >  Daveed
>
> Where in the working papers does it say this?  They appear to me to be
> introduced in 14.8 [temp.arg], which as far as I can tell applies to
> both functions and classes.  I see nothing in 14.10 [temp.fct] which
> disallows them--what did I miss?

That's right: there's nothing in the WP that disallows default
template arguments for functions; there's also nothing there, though,
that says what their semantics would be if they were allowed.

The committee had to choose between defining the semantics of default
template arguments for functions, or simply disallowing them; it chose
the latter.  This decision was made at the Santa Cruz meeting, so it
isn't yet reflected in a new version of the WP.

I don't think there would have been any technical problems with
allowing default template arguments for function templates; it's just
that it would have required a fair amount of work to come up with a
clear set of rules.  (I can think of at least three or four issues
that would have had to be resolved, which probably means that there
are another dozen that I haven't thought of.)  The general feeling was
that the feature wouldn't have been important enough to be worth that
much effort.
--
Matt Austern
SGI: MTI Compilers Group
austern@isolde.mti.sgi.com
---
[ 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                             ]





Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/04/19
Raw View
>>>>> "BF" == Brian Michael Freyburger <freyburg@wavelet.stanford.edu> writes:
I (Daveed) wrote:
>>  It's a non-issue since default template arguments cannot be
>> specified for function templates.

BF> Where in the working papers does it say this?  They appear to me
BF> to be introduced in 14.8 [temp.arg], which as far as I can tell
BF> applies to both functions and classes.  I see nothing in 14.10
BF> [temp.fct] which disallows them--what did I miss?

Good point ;^P It's not in the April '95 CD, apparently :^( However,
it's in the Templates chapter as revised for the Santa Cruz meeting.
Sorry for the confusion, I didn't realize this was this recent.

BF> Why are they disallowed?  Are there any technical difficults?

The problem you mentioned maybe ;-)

 Daveed


[ 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                             ]





Author: freyburg@glitch.stanford.edu (Brian Michael Freyburger)
Date: 1996/04/15
Raw View
Reading section 14.10.2 of the January WP, I can not figure out
whether the following function call can be deduced:

template <class S, class T = S>
int foo(S s, T t = T());

int bar()
{
  foo(1);
}

The compiler must deduce that S is int from the one argument
specified, then determine that T is int from the default parameter for
T, and finally determine that the second argument has the default
value of int().


Thanks,

Brian Freyburger


[ 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                             ]