默认参数必须在编译时绑定 - 为什么? [英] Default arguments have to be bound at compiled time - why?

查看:120
本文介绍了默认参数必须在编译时绑定 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C ++这样设计?

Why was C++ designed like this?...

(这个问题不同,但与

不可能:此指针为默认参数为什么?

推荐答案

实际上,这并不完全准确。限制是:

Actually, that's not completely accurate. The restrictions are:


7)局部变量不应在默认参数中使用。 [示例:

7) Local variables shall not be used in a default argument. [ Example:



void f() {
int i;
extern void g(int x = i); //error
// ...
}




-end example]

—end example ]

8)关键字 this 不得用于成员函数。 [Example:

8) The keyword this shall not be used in a default argument of a member function. [ Example:



class A {
void f(A* p = this) { } // error
};

因此,

例如,以下内容有效:

int a = 1;
int f(int);
int g(int x = f(a)); // default argument: f(::a)
void h() {
  a = 2;
  {
    int a = 3;
    g(); // g(f(::a))
  }
}

g 将使用值 f(2)调用,这不是编译时常量。这是一个直接来自标准的例子。

g will be called with the value f(2), which isn't a compile-time constant. This is an example straight from the standard.

这样的原因是通常的:没有一个提案,或者它被拒绝,被认为不是必要或太难实施。

The reasons it's like this is the usual: there either wasn't a proposal for it, or it was rejected, deemed not necessary or too difficult to implement.

这篇关于默认参数必须在编译时绑定 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆