Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #5513

Re: Template argument determination

Newsgroups comp.lang.c++
From "A. Bolmarcich" <aggedor@earl-grey.cloud9.net>
Subject Re: Template argument determination
References <irh227$8du$1@dont-email.me> <slrnitob96.77f.aggedor@earl-grey.cloud9.net> <irhdh7$na0$1@dont-email.me>
Message-ID <slrnitqfiq.10rf.aggedor@earl-grey.cloud9.net> (permalink)
Date 2011-05-25 12:37 -0500

Show all headers | View raw


On 2011-05-24, Victor Bazarov <v.bazarov@comcast.invalid> wrote:
> On 5/24/2011 6:11 PM, A. Bolmarcich wrote:
>> On 2011-05-24, Victor Bazarov<v.bazarov@comcast.invalid>  wrote:
>>> OK, try compiling this:
>>>
>>>     template<class T>  struct FromT {
>>>         FromT(T) {}   // here is your non-explicit c-tor
>>>     };
>>>
>>>     template<class T>  void foo(FromT<T>  ft) {}
>>>
>>>     int main() {
>>>        foo(42);
>>>     }
>>>
>>> And then explain.  Then change the line in 'main' to
>>>
>>>        foo<int>(42);
>>>
>>> and try again.  And if the results are different, try to explain why
>>> they are.
>>
>> For original program with foo(42), the template for foo is not instantiated
>> because the compiler is not able to deduce the template argument to use for
>> T.
>
> Perhaps I didn't understand your statement.  I seems you said that the 
> compiler was supposed to use the conversion as basis for deducing the 
> template argument from the function call with 42.  You snipped this:

I did not say that conversion by construtor was used when deducing
template arguments.  It is used after the specific function to call
has been determined.  Determining the specific function to call may
involve instantating function templates using deduced template
arguments.

I snipped from your post all but what you wrote and explained the
difference between using

  foo(42)

and

  foo<int>(42)

in the example you wrote.  With foo(42), the function template
argument was neither explicitly specified nor deduced, and the
template was not instantiated.

>> On 2011-05-24, Ruben Safir <ruben@mrbrklyn.com> wrote:
>>
>>> > Bingo - thanks for taking the time to give me this explanation.  But its not
>>> > capable of doing this implicit conversation unless T is defined as an int.
>>> > The compiler couldn't deduce this otherwise.
>> A compiler should do the implicit conversion as long as the parameter type has
>> an accessible
>>
>> - non-explicit constructor that can take a value of type T as the only
>>   argument, or
>> - conversion function of the form: operator T()
>
> Let me explain what I am reading here.  Ruben: "compiler cannot use any 
> conversion unless T is defined".  You: "A compiler should do the 
> conversion." - as if it doesn't matter whether type is deduced or not.

What Ruben wrote was: "But its not capable of doing this implicit
conversion unless T is defined as an int."  Your reading missed
some of the words Ruben wrote.  I wrote that an implicit
conversion from function arguments to function parameters should be
done even if T is defined as something other than an int.  For
example,

  template <class T>
  struct List {
    List() {}
  };

  template <class T>
  struct X {
    X(T x) {}
    X(List<T> x) {}
  };

  template <class T>
  void foo(X<T> x) {}

  int main() {
    List<int> il;

    foo<int>(42);
    foo<List<int> >(il);
  }

For the call foo<int>(42), T is defined as an int and the signature
of the instantiated function is

  foo<int>(X<int>)

As part of calling that function, the argument value 42 is implicitly
converted to the X<int> parameter, using the X(int) constructor.

For the call foo<List<int> >(il), T is defined as a List<int> and the
signature of the instantiated function is

  foo<List<int> >(X<List<int> >)

As part of calling that function, the argument value il is implicitly
converted to the X<List<int> > parameter, using the X(List<int>)
constructor.  In this case, a conversion by constructor is done even
though T is not defined as an int.

[snip]

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-23 20:31 +0000
  Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-24 02:50 +0000
    Re: Template argument determination Michael Doubez <michael.doubez@free.fr> - 2011-05-24 03:48 -0700
    Re: Template argument determination "Balog Pal" <pasa@lib.hu> - 2011-05-24 13:06 +0200
      Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-24 14:44 +0000
        Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-24 15:04 +0000
          Re: Template argument determination "Balog Pal" <pasa@lib.hu> - 2011-05-24 17:33 +0200
          Re: Template argument determination Victor Bazarov <v.bazarov@comcast.invalid> - 2011-05-24 11:43 -0400
  Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-24 12:32 -0500
    Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-24 18:08 +0000
      Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-24 14:32 -0500
        Re: Template argument determination Victor Bazarov <v.bazarov@comcast.invalid> - 2011-05-24 15:48 -0400
          Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-24 17:11 -0500
            Re: Template argument determination Victor Bazarov <v.bazarov@comcast.invalid> - 2011-05-24 19:04 -0400
              Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-25 12:37 -0500
                Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-26 16:12 +0000
                Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-26 13:25 -0500
            Re: Template argument determination Ruben Safir <ruben@mrbrklyn.com> - 2011-05-25 02:47 +0000
              Re: Template argument determination "Balog Pal" <pasa@lib.hu> - 2011-05-25 10:06 +0200
              Re: Template argument determination "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-05-25 12:40 -0500

csiph-web