# How C++ Resolves a Function Call

DevFeed: [How C++ Resolves a Function Call](<https://devfeed.tech/articles/how-c-resolves-a-function-call-21020.md>)

Original publisher: [Read original article](<https://preshing.com/20210315/how-cpp-resolves-a-function-call>)

Author: Jeff Preshing

Published: 2021-03-15T12:10:00Z

Content type: article

Language: en

Sources: [Jeff Preshing](<https://devfeed.tech/sources/jeff-preshing.md>)

Topics: [Tutorial](<https://devfeed.tech/topics/tutorial.md>), [Algorithms](<https://devfeed.tech/topics/algorithms.md>), [Type Argument](<https://devfeed.tech/topics/type-argument.md>), [Jule](<https://devfeed.tech/topics/jule.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [built-in](<https://devfeed.tech/tags/built-in.md>), [c](<https://devfeed.tech/tags/c.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [call](<https://devfeed.tech/tags/call.md>), [career](<https://devfeed.tech/tags/career.md>), [compile](<https://devfeed.tech/tags/compile.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [features](<https://devfeed.tech/tags/features.md>), [function](<https://devfeed.tech/tags/function.md>), [functions](<https://devfeed.tech/tags/functions.md>), [namespaces](<https://devfeed.tech/tags/namespaces.md>), [operator](<https://devfeed.tech/tags/operator.md>), [operators](<https://devfeed.tech/tags/operators.md>), [runtime](<https://devfeed.tech/tags/runtime.md>), [sort](<https://devfeed.tech/tags/sort.md>), [string](<https://devfeed.tech/tags/string.md>), [strings](<https://devfeed.tech/tags/strings.md>), [template](<https://devfeed.tech/tags/template.md>), [templates](<https://devfeed.tech/tags/templates.md>)

## AI overview

C++ resolves function calls at compile time through a sequence of steps: name lookup gathers candidates, including functions found through argument-dependent lookup for unqualified calls; template argument deduction and substitution turn viable templates into functions or remove them when deduction or substitution fails (SFINAE); and overload resolution filters by argument compatibility and constraints before selecting the best viable function. The example shows a namespace function and a function template with identical parameter types; the non-template wins the tie. Better argument conversions take precedence over template status, and a more specialized template can win when earlier rules do not decide the call. If no unambiguous best candidate exists, compilation fails. The article presents a high-level explanation and notes that detailed rules for lookup, conversions, deduction, and specialization are omitted.

## Source excerpt

C is a simple language. You're only allowed to have one function with each name. C++, on the other hand, gives you much more flexibility: You can have multiple functions with the same name (overloading). You can overload built-in operators like + and ==. You can write function templates. Namespaces help you avoid naming conflicts. I like these C++ features. With these features, you can make str1 + str2 return the concatenation of two strings. You can have a pair of 2D points, and another pair of 3D points, and overload dot(a, b) to work with either type. You can have a bunch of array-like classes and write a single sort function template that works with all of them. But when you take advantage of these features, it's easy to push things too far. At some point, the compiler might unexpectedly reject your code with errors like: error C2666: 'String::operator ==': 2 overloads have similar conversions note: could be 'bool String::operator ==(const String &) const' note: or 'built-in C++ operator==(const char *, const char *)' note: while trying to match the argument list '(const String, const char *)' Like many C++ programmers, I've struggled with such errors throughout my career. Each time it happened, I would usually scratch my head, search online for a better understanding, then change the code until it compiled. But more recently, while developing a new runtime library for Plywood, I was thwarted by such errors over and over again. It became clear that despite all my previous experience with C++, something was missing from my understanding and I didn't know what it was. Fortunately, it's now 2021 and information about C++ is more comprehensive than ever. Thanks especially to cppreference.com, I now know what was missing from my understanding: a clear picture of the hidden algorithm that runs for every function call at compile time. This is how the compiler, given a function call expression, figures out exactly which function to call: may performargument-dependentloo