How do automatic arguments work internally?
Consider the code,
#include <cstdio>
auto f(const auto &loc){
printf("Location: %p\n", &loc);
}
int main()
{
auto x {1};
auto y {2.3};
f(x);
f(y);
}
compile with g++ -std=c++14 dummy.cpp
Question:
For template functions, the type is explicitly mentioned ( f<int>(2)
) at compile time.
How does a function f
accept arguments of a different type?
source to share
The Technical Specification of the Concept "Function"
auto f(const auto &loc){
printf("Location: %p\n", &loc);
}
is actually template
(shorthand function template declaration) and is equivalent to (but shorter and easier to read)
template<typename T>
void f(const T&loc){
printf("Location: %p\n", &loc);
}
Note, however, that the form using auto
is not yet part of any C ++ standard, but only a Technical Concept Specification for concepts and constraints that look very powerful (but AFAIK only supported by GNU gcc version ≥6.1 with option -fconcepts
).
source to share