1. Consider the function template: template <typename T, typename U> void pair(T a, U b) { /* ... */ } When calling pair(5, 3.14), what types are deduced for T and U respectively?
template <typename T, typename U>
void pair(T a, U b) { /* ... */ }
double
int
a
b
2. Given the following code: template <typename T> std::string Version(T in) { return "Generic"; } template <> std::string Version(std::string in) { return "String"; } What would happen if you ran Version("test")?
template <typename T>
std::string Version(T in) { return "Generic"; }
template <>
std::string Version(std::string in) { return "String"; }
Version("test")
"test"
std::string
const char *
"Generic"
3. Which of the following statements about reference collapsing is TRUE?
4. How does the keyword mutable modify a lambda?
mutable
5. Is there a problem with the following code? Why or why not? auto iter = std::ranges::min_element(std::vector<int>{3, 1 , 2}); std::cout << *iter << std::endl;
auto iter = std::ranges::min_element(std::vector<int>{3, 1 , 2});
std::cout << *iter << std::endl;
6. Template parameters are processed in two phases. Which of the following happens in the SECOND phase?
7. Given the following function template: template <typename T> bool CompareToZero(T in) { return in == static_cast<T>(0); } Which function call will NOT cause a compilation error?
bool CompareToZero(T in) {
return in == static_cast<T>(0);
}
CompareToZero<std::string>("12");
CompareToZero('c');
CompareToZero(0, 0);
CompareToZero(std::vector<int>{0});
8. Which of that following is a potential downside of excessive use of templates, even when fully adhering to the C++ standard?
9. What is a template specialization?
10. When does capturing occur in a lambda?
Click Check Answers to identify any errors and try again. Click Show Answers if you also want to know which answer is the correct one.