ProgrammingVery hardProduct-company placement · C++
What is printed?
What is printed?
template <class T> void f(T) { cout << "1"; }
template <> void f(int*) { cout << "2"; } // explicit specialisation
template <class T> void f(T*) { cout << "3"; }
int main() { int* p = nullptr; f(p); }- A. 2
- B. 3
- C. 1
- D. Compile-time error: ambiguous call
Correct answer: B. 3
Overload resolution looks only at primary templates: f(T*) is more specialised than f(T), so it wins. The explicit specialisation f<int*> belongs to f(T), which was not chosen, so it is never considered.
More OOP with C++ and Java questions
- What is printed? cpp template <int N> struct F { static const int v = N F<N - 1>::v; }; template <> struct F<0> { static const int v = 1…
- What is printed? cpp struct M { M() { throw 1; } }; struct S { M m; S() try : m() { } catch (int) { cout << "S"; } }; int main() { try { S…
- What is printed? cpp void f() noexcept { } void g() { } int main() { cout << noexcept(f()) << noexcept(g()); }
- What happens when this is compiled? cpp int main() { try { throw 1; } catch (...) { cout << "any"; } catch (int) { cout << "int"; } }
- What is printed? cpp int main() { try { throw runtimeerror("disk full"); } catch (const exception& e) { cout << e.what(); } }
- What is printed? cpp struct E { }; int main() { try { throw new E(); } catch (E&) { cout << "ref"; } catch (E) { cout << "ptr"; } }