2

Example: Assuming f to be a template function, having two arguments:

f (1, 2) In this call, does the template function assume that its arguments are int, or short, or anything else?

EDIT 1:

The template function declaration:

template <typename dataTypeA, typename dataTypeB> dataTypeB functionX (dataTypeA argA, dataTypeB argB)

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • @Kerrek SB You can assume that the template function just prints out the two arguments. – Aquarius_Girl Aug 25 '11 at 10:35
  • 2
    A bit nitpicking: It is called "function template" because it is a template, not a function. Recalling this fact sometimes helps to understand some issues you might run into ("but I give it a function, why does the compiler complain" "no, you give it a template") – PlasmaHH Aug 25 '11 at 10:50

2 Answers2

7

The template does not assume anything. The literals are of type int, and that will be the type that the compiler deduces.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • and why not of type `short`? `1` and `2` fit in shorts too. Is this compiler dependent? – Aquarius_Girl Aug 25 '11 at 10:36
  • 3
    @Anisha: No, the *literal* `1`, as written in your source code, has a type, and that type is `int`. If you write `1u` the type is `unsigned int`, and `1l` is `long int` etc. I don't think there's a `short` literal. – Kerrek SB Aug 25 '11 at 10:37
  • @Kerrek that was helpful, thanks. I didn't know that it is permissible to write `1u` too. So, the only way to pass a `short` value will be like `short h = 10`? See if you can write that as a separate somewhat detailed answer, I'll select it. – Aquarius_Girl Aug 25 '11 at 10:45
  • You can create a short in place. OK, let me spin up my crystal ball and guess what you might be doing. – Kerrek SB Aug 25 '11 at 10:48
  • 1
    @Anisha: you can also force the type, either by providing the type argument to the template `f(5)` (assuming `template void f( T );`) or by casting the argument: `f( (short)5 )` – David Rodríguez - dribeas Aug 25 '11 at 10:48
  • It's odd that there's no `short` literal since there is a `char` literal and that makes `short` the only integer type with no literal. – Flexo Aug 25 '11 at 10:49
  • @Kerrek and what was this pun for? `OK, let me spin up my crystal ball and guess what you might be doing.` – Aquarius_Girl Aug 25 '11 at 10:50
  • @David - you could also use `f(short(5))` syntax. – Flexo Aug 25 '11 at 10:50
  • David, your comment was helpful, thanks. What is the literal for a `char`? – Aquarius_Girl Aug 25 '11 at 10:52
  • 1
    `char` literal is just `'a'` or similar. – Flexo Aug 25 '11 at 10:53
  • @Anisha: the pun is because I have no idea what your actual code or problem is, so I can merely make some general statement about function templates and argument deductions and hope that it applies to your situation. The crystal ball is often cloudy, but sometimes it works :-) – Kerrek SB Aug 25 '11 at 10:56
  • @Kerrek I didn't present the code, since I thought that it is a "conceptual" matter. What the function does from inside doesn't matter, IMO. – Aquarius_Girl Aug 25 '11 at 10:58
  • 2
    @Anisha: No no, you misunderstand. The function definition is indeed irrelevant. But the function template declaration would have been immensely useful. From your question, your function template could have been `template char foo(T, int)`, `template bool foo(T, T)`, or even `template T foo(int, int)`, and there's no way of knowing, and they all have different answers. I guessed the most likely one, but you could just have made this clear in the question. – Kerrek SB Aug 25 '11 at 11:01
  • @Kerrek alright, I misunderstood you, but you didn't state it clearly too that you wanted the "declaration" only. I'll edit the OP. :) – Aquarius_Girl Aug 25 '11 at 11:03
  • 1
    @Anisha: Welcome to SO :-) When people say "post the code", they mean, "post *some* representative, minimal code that makes the question clear", they don't want you to post your entire source file :-) – Kerrek SB Aug 25 '11 at 11:07
3

As @David already said, as far as your question is concerned, there's no such thing as "making assumptions". The literals simply have types, which are the types that a function template may use for type deduction. Remember that conversions considered as part of the template matching, though!

So, let's say you have this function template:

template <typename T> void foo(T x, T y);

Then if you call foo(1, 2), this will be called with T = int.

If you say foo(1u, 2u), the deduction is T = unsigned int.

If you say anything mixed like foo(1u, 2), there is no preferred match and the compiler will report an error.

Since there is no short literal in C or C++, if you want to explicitly call the function foo<short>, you can either say so, or create temporary explicitshort arguments:

foo<short int>(3, 4);
foo<short int>(3u, 4l);  // also OK because of conversion
foo(short(3), short(4)); // deduction

Update: In light of your edit, note that since you're only matching one argument per template parameter, you won't have any trouble with ambiguous matching.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • The one that puzzled me was `foo(1u, 1l);` as no preferred match since `T = long` would work without any loss. (But makese sense since you *could* have `sizeof(long) == sizeof(unsigned)` on some other platform) – Flexo Aug 25 '11 at 11:00
  • 2
    @Awoodland: I'm sure there's a perfect standardese explanation involving the matching rules and implicit conversion, but I'm not 100% sure on those, so hopefully someone will come and state that :-) @Anisha: One nitpick, the literal `L'a'` is of type `wchar_t`, *not* `short int`, and likewise for `L"hello"` arrays. That's a mistake on that site, beware. I just noticed it and thought that's important to point out for your situation. – Kerrek SB Aug 25 '11 at 11:05
  • Indeed, it was important to point out, thanks. Do point out if there is something else wrong too. :) – Aquarius_Girl Aug 25 '11 at 11:08
  • 1
    @Anisha: Well, the site is missing the sexy new C++11/C1x [Unicode string literals](http://stackoverflow.com/questions/6796157/unicode-encoding-for-string-literals-in-c0x) of which I'm a great fan, though they may not be of immediate use to you. Also C++11 has overloadable (postfix) literals, in theory. – Kerrek SB Aug 25 '11 at 11:10
  • Thanks, and please add that link to your OP, since people may miss reading the comments. – Aquarius_Girl Aug 25 '11 at 11:12
  • Also, `unsigned short int : L'ab'` is shown for unsigned short in that link, why are they referring 'a' 'b' there? why not, '1' '2'? And they don't have anything for `short`? – Aquarius_Girl Aug 25 '11 at 11:52
  • 1
    @Anisha: You know what, please ignore the first three rows of that table, that's all misleading and wrong. You can't have multi-character character literals, and "ASCII" is also wrong. I'd replace the link by a better one if I had one. In any event, there is no `short` literal. I guess it's a C legacy, in C every small integral was promoted to `int` (e.g. in C, 'a' is an int, not a char) at the slightest provocation. Presumably nobody felt the need to refit C++ with a short int literal. Double check your promotion rules, though, especially when doing arithmetic. – Kerrek SB Aug 25 '11 at 11:57
  • 1
    @Anisha: The [IBM documentation](http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlcpp8a.doc/language/ref/conref.htm) is much better, but it's also a lot harder to read. If you find a good summary of C++ literal constant syntax, do say. – Kerrek SB Aug 25 '11 at 12:06
  • Thanks for the effort :hattip: I have put the question here: http://www.linuxquestions.org/questions/programming-9/c-literal-for-short-and-unsigned-short-899372/ Lets see if they can answer something. – Aquarius_Girl Aug 25 '11 at 12:09