Not possible: this pointer as a default argument. Why?

瘦欲@ 提交于 2019-11-29 07:11:32
Eric

Default argument values are bound at compile time.

"this" is only defined at run time, so can't be used.

See here for a fuller explanation: Must default function parameters be constant in C++?

Others have already commented on the reason this doesn't work. From one of the comments:

"...The expression can combine functions that are visible in the current scope, constant expressions, and global variables. The expression cannot contain local variables or non-static class-member variables..."

You could use optional to eliminate the extra function although I'm not sure it's clearer:

void f( boost::optional<int> i = boost::none ) { if(!i) i = j; ... }

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