Search⌘ K
AI Features

- Example

Understand how C++ templates resolve function calls through an example focusing on template lookup behavior. Learn how functions for different types are selected during instantiation and how template details affect function access in your code.

We'll cover the following...

Example: Template Lookup

C++
// templateLookup.cpp
#include <iostream>
void g(double) { std::cout << "g(double)\n"; }
template<class T>
struct S {
void f() const {
g(1); // non-dependent
}
};
void g(int) { std::cout << "g(int)\n"; }
int main(){
g(1); // calls g(int)
S<int> s;
s.f(); // calls g(double)
}

Explanation

If we access the defined functions g with double or ...