Template Function Requirements ?

A few days ago, I had an exam, the question was:

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......

Should I prepare for the 2nd c++ course ? :(
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.
I don't understand the question.
You and me both, helios.

T is always "a must". It's part of the template.

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.
the correct answer is 3

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...

edit: added comment regarding answer 2
Last edited on
... where T is a must

Maybe "where an explicit T during invocation is a must" would have been clearer?
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


I don't see any way you can make 2 be non-ambiguous.
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.
Just to let everyone know, the "correct" answer was 4 :S

Why ? Who knows, he won't answer :(
func2<A> and func2<B> aren't overloads, though. They're different functions with different names.
closed account (z05DSL3A)
Very badly worded question.

My reading of it is:

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?"
closed account (z05DSL3A)
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);
Unless you're looking at Grey Wolf's understandable interpretation.

This compiles fine:
1
2
3
4
5
6
7
8
9
10
template<typename T>
void Func()
{
	cout << "Hi, Earth" << endl;
}

int main()
{
	Func<double>();
}


Proving that, for Grey Wolf's interpretation, 4 is correct.
ah, yeah Grey Wolf's response does make the most sense.

Still, though, very poorly worded.
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)..
Topic archived. No new replies allowed.