A C++ program defines the function template below and then makes seven calls to it in main.
template < typename T > T mid(T a, T b) { return a + (b - a) / 2; } mid(2, 10); mid(1.5, 4.0); mid('a', 'e'); mid(6, 9); mid(2.0f, 3.0f); mid(0.25, 0.75); mid(100L, 300L);
How many distinct instantiations of mid does the compiler generate? Give your answer as a whole number.
Unlock full course notes by signing up
From template call to machine code
A C++ source file defines template < typename T > T scale(T x, T k) { return x * k; } and later calls scale(2.5, 4.0). Put the events that turn this call into running code in the order in which they happen during compilation and linking.
Tracing a generic method and counting compiled copies
Consider this Java generic method and the calls that follow it.
Find the value stored in x, the value stored in y, the number of compiled method bodies for countMatches in the Java class file, and the number of instantiations a C++ compiler would generate if the method were rewritten as a function template and called once with an int array and once with a std::string array. Give each answer as a whole number.
Checking bounds and generic assignments
A sensor application uses the following Java class. Integer and Double extend Number, whereas String does not.
Consider each declaration independently inside a method in the same package. Enter yes if the declaration compiles or no if it does not. No casts or omitted type arguments are allowed.
Reading and writing through bounded wildcards
A data-processing application uses this Java class. The ? in a generic type is a wildcard representing an unknown type argument.
Inside a method, the following initialized parameters are available:
Consider each candidate statement independently. Classify it using its compile-time type, without changing the parameters or adding casts. The cast already shown in one candidate is part of that candidate.