+ 3

[C++] why it doesn't increment if argument is a float?

Result is "223": template<class T> void f(T){ static int i = 1; cout << ++i; } int main(){ f(1); f(1.0); f(1); }

1st Dec 2021, 8:32 AM
Paolo De Nictolis
Paolo De Nictolis - avatar
1 Answer
+ 5
Because template expansion is done during compile time and not at runtime. So at runtime there exist 2 seperate functions "f ()" ( one for float and one for int ) and f(1.0) is calling different function than f(1)
1st Dec 2021, 8:40 AM
Arsenic
Arsenic - avatar