In C++, when I pass a value into a function, is it always converted to appropiate type?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:57:47

问题


If I have a function like void func(size_t x) and I call the function func(5), is 5 immediately converted to size_t type? Does this hold generally for all types?

I ask because I swear I've seen people write code where they do stuff like func(5.0) (passing 5 as a double) or func(0UL) (passing 0 as an unsigned long int). Is this really necessary? Can't we just pass in whatever we want, and C++ will treat it as the type that I used to define the function?


回答1:


If there is an implicit conversion between the argument type and the type passed to the function then the argument will be converted. If there isn't one, like trying to pass a std::list to a function that expects a std::vector then it won't and you will get an error.

One reason to use a specific literal, like func(5.0) or func(5UL) is if func is a template function, or is overloaded. If func is a template or is overloaded (for the appropriate types) then func(5.0), func(5UL) and func(5) would generate/call 3 different functions. One for a double, one for a unsigned long and one for an int. This could be meaningful as there could be specializations/overloads handling these types differently.

You also run into cases like std::accumulate whose third parameter, the accumulator, has its own template type. Lets say you want to sum all of the elements in a std::vector<double>. If you use

std::accumulate(vector.begin(), vector.end(), 0)

then you would get a different result than

std::accumulate(vector.begin(), vector.end(), 0.0)

because the first call uses an int to store the sum which will truncate each time, while the latter uses a double and you will get the expected result.



来源:https://stackoverflow.com/questions/52450363/in-c-when-i-pass-a-value-into-a-function-is-it-always-converted-to-appropiat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!