What do template functions require (meaning , where T is a must) ?
1) template <class T> void func(T& t);
2) template <class T> T& func();
3) template <class T> void func() { T t ,...... }
4) None of the above
I marked 1 :S, cause I thought that's how you "define" the function to be of the type you currently use......
1, 2, and 3 are all valid, but 1 allows the compiler to deduce the template arguments for itself. For the other two, the compiler wouldn't be able to tell what the template parameter should be.
Unless they mean explicitly specifying T in the function call, in which case the answer would be 2 & 3 since T cannot be deduced from the argument list.
in 1 and 2, T is part of the signature of the function - hence the compiler can infer the correct implementation to build and call (2 is a bit tricky - it depends on the situation - in certain cases, you may need to explicitly specify the class T during the call if there is possibility for ambiguity due to implicit conversions)
however, in 3, because T is not part of the signature, the compiler has no idea which template function to call unless you explicitly call func<int>() or func<Foo>();
but I have to agree with helios and Disch - I had to read the question several times before inferring your instructor's intended meaning
he should've written the question as:
Which of the following template functions always requires explicit specification of T in its invocation?
I would complain to have the question thrown out for lack of clarity
edit: hint - when doing questions like this, always try to think like the compiler, as hard as that may be...
you are right - my bad - I couldn't call it without the parameter either - both firedraco and Disch are right - 2 and 3 both seem to need an explicit T...
...I wonder what the teacher is going to say the answer is
Well, functions can't be overloaded by their return type only, so maybe it's not possible to func2<A>() and then func2<B>()? Although then we'd be going way too far from the original question.
When you define a function template, what elements are required to be template elements
1) parameters
2) return type
3) local variables
4) None of the above
So the answer is 4, as it doesn't have to any particular one of them.
That was my first interpretation, as well. I discarded it for being too stupid. It's like asking "when does the language force you to declare something as int?"
It is not stupid to ask a question about the perceived requirements of a function template. The question itself requires some thinking about, may by design or maybe it is just a crap question.
What kind of idiot is designing these exams?
One would expect from someone in the computer science department who is being tasked with something like that to be able to formulate a clear question.
4) is undoubtely correct - you could have always written template <class U> void func(U& u);
Just to be clear, I'm from Israel and the test wasn't in English, so you can blame my English, but I see you guys understood the question (I failed with :( )
Stupid Dr. (or students) , 53% failures in this exam (I got 70/100 where 60 passes)..