【发布时间】:2012-02-27 03:19:01
【问题描述】:
我正在编写一个函数foo(),它接受2 个const char*s 作为参数,pBegin 和pEnd。 foo() 传递了一个以空字符结尾的字符串。默认情况下,pEnd 指向字符串的\0(最后一个字符)。
void foo (const char *pBegin,
const char *pEnd = strchr(pBegin, 0)) // <--- Error
{
...
}
但是,我在上面的行中收到错误:
error: local variable ‘pBegin’ may not appear in this context
为什么编译器不允许这样的操作?潜在的问题是什么?
【问题讨论】:
-
不同的调用约定以不同的顺序推送参数。一些典型的(
cdecl,stdcall)从右向左推,所以在你的例子中,pEnd需要在pBegin之前进行评估,我认为这没有意义......有一个小测验这里:en.wikipedia.org/wiki/X86_calling_conventions
标签: c++ function compiler-errors language-lawyer default-arguments