Default argument vs overloads in C++

后端 未结 4 1327
眼角桃花
眼角桃花 2020-12-16 14:24

For example, instead of

void shared_ptr::reset() noexcept;
template 
void shared_ptr::reset(Y* ptr);

one may think of

4条回答
  •  忘掉有多难
    2020-12-16 15:09

    There is a fundamental difference between an overload and a default pointer:

    • the overload is self contained: the code in the library is completely independent of the calling context.
    • the default parameter is not self contained but depend on the declaration used in the calling context. It can be redefined in a given scope with a simple declaration (e.g. a different default value, or no default value anymore.

    So semantically speaking, the default value is a short-cut embeded in the calling code, whereas the overload is a meaning embedded in the called code.

提交回复
热议问题