6 Replies - 1559 Views - Last Post: 21 April 2010 - 01:51 PM Rate Topic: -----

#1 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

template type inference

Posted 21 April 2010 - 08:13 AM

In this recent thread:
http://www.dreaminco..._0&#entry996120
we see that templated functions can be called without specifying the template's type parameter and the compiler will infer the type from the function's argument list. You learn something new every day -- I thought you always have to provide the type parameter(s).

Question is, when do you and when do you not have to specify the type? For example, why, in the following simple program, does the compiler refuse to infer the type of vector from the type of arguments I supply to the constructor?

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int ints[] = {1,2,3,4};
//  vector<int> vec( ints, ints+4 );
  vector vec( ints, ints+4 ); // why isn't vector type inferred from args?
  for( int i = 0; i < vec.size(); ++i )
    cout << vec[i] << endl;
}


It seems trivial for the compiler to figure out that I want a vector<int>, yet it refuses to compile unless I specify that. So why does j7x's code (in the linked thread) compile without specifying type in the function calls? Does type inference work only for function templates but not for class templates? Or is there some special pattern to j7x's functions that I'm not seeing?

Is This A Good Question/Topic? 0
  • +

Replies To: template type inference

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: template type inference

Posted 21 April 2010 - 08:26 AM

Simple answer: you always have to specify the template type for classes/structs. "vector" is a class, so it must have the template type explicitely specified. For functions, you don't have to (most of the times).
Was This Post Helpful? 1
  • +
  • -

#3 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: template type inference

Posted 21 April 2010 - 08:35 AM

I say that infer is probably not the best choice of words. Template is a compile-time feature so that is why you are not able to use as you want the vector class. In case of functions the template parameter is resolved from the context of the function call and thus the type could be omitted. This link is usefull.

This post has been edited by muballitmitte: 21 April 2010 - 08:39 AM

Was This Post Helpful? 0
  • +
  • -

#4 yfronto  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 30
  • Joined: 03-November 08

Re: template type inference

Posted 21 April 2010 - 08:37 AM

I'd say that the compiler can't infer because you can write a ctor for a template class that takes arguments that aren't the same datatype as the datatype of the class. Let's say you make a template class for a student record and the ctor accepts a bool representing whether or not the student is currently enrolled, along with a char representing male or female, but the datatype for the class is another class (freshman, sophomore, etc). Specifying MyClass<class Sophomore> NewStudent(true, 'M') would work, but MyClass NewStudent(true, 'M') would not, because the compiler cannot infer the data type of the class based on the arguments to the ctor.
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: template type inference

Posted 21 April 2010 - 09:28 AM

@sarmanu: Simple answers are the best kind, when they are correct. It seems that your's is. ;)

@muballitmitte: I think infer was a very precise choice. I offer you this from Merriam-Webster Online and this from Wikipedia. Thanks for that link to the IBM document. On the first line it says "When you call a template function, you may omit ... " and it doesn't say anything comparable about classes/structs, so we can infer that this only applies to functions.

@yfronto: examples of specific instances where it can't be done don't prove that it can never be done. It's just as easy to come up with an example in which the compiler theoretically could infer the type (such as the one I posted initially), but it doesn't. Also, as the IBM link shows, there are situations where the compiler can't infer the type to use for a function template, but in those cases where it can do so, it does.

This post has been edited by r.stiltskin: 21 April 2010 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#6 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: template type inference

Posted 21 April 2010 - 09:53 AM

View Postr.stiltskin, on 21 April 2010 - 08:28 AM, said:

@muballitmitte: I think infer was a very precise choice. I offer you this from Merriam-Webster Online and this from Wikipedia. Thanks for that link to the IBM document. On the first line it says "When you call a template function, you may omit ... " and it doesn't say anything comparable about classes/structs, so we can infer that this only applies to functions.


I am sorry if it seemed like I was trying to quarrel. I was only trying to say that while it`s ok to say that we infer it could be better to say that the compiler resolves.

This post has been edited by muballitmitte: 21 April 2010 - 09:53 AM

Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: template type inference

Posted 21 April 2010 - 01:51 PM

View Postmuballitmitte, on 21 April 2010 - 12:53 PM, said:

I am sorry if it seemed like I was trying to quarrel. I was only trying to say that while it`s ok to say that we infer it could be better to say that the compiler resolves.


No problem, I didn't take it as a quarrel.
quarrel == bad
argue == good
;)


If you go to Google Scholar (or even regular Google) and search on "type inference" you will get page after page of on-point hits. Search on "type resolution" and you get mostly irrelevant material. "Overload resolution" fares a little better, but it's still too general. So I still think inference is the better term.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1