- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
例如,我不能这样写:
class A
{
vector<int> v(12, 1);
};
我只能这样写:
class A
{
vector<int> v1{ 12, 1 };
vector<int> v2 = vector<int>(12, 1);
};
为什么这两种声明语法之间存在差异?
最佳答案
这一选择背后的基本原理在相关的 proposal 中明确提及。对于非静态数据成员初始值设定项:
An issue raised in Kona regarding scope of identifiers:
During discussion in the Core Working Group at the September ’07 meeting in Kona, a question arose about the scope of identifiers in the initializer. Do we want to allow class scope with the possibility of forward lookup; or do we want to require that the initializers be well-defined at the point that they’re parsed?
What’s desired:
The motivation for class-scope lookup is that we’d like to be able to put anything in a non-static data member’s initializer that we could put in a mem-initializer without significantly changing the semantics (modulo direct initialization vs. copy initialization):
int x();
struct S {
int i;
S() : i(x()) {} // currently well-formed, uses S::x()
// ...
static int x();
};
struct T {
int i = x(); // should use T::x(), ::x() would be a surprise
// ...
static int x();
};
Problem 1:
Unfortunately, this makes initializers of the “( expression-list )” form ambiguous at the time that the declaration is being parsed:
struct S {
int i(x); // data member with initializer
// ...
static int x;
};
struct T {
int i(x); // member function declaration
// ...
typedef int x;
};
One possible solution is to rely on the existing rule that, if a declaration could be an object or a function, then it’s a function:
struct S {
int i(j); // ill-formed...parsed as a member function,
// type j looked up but not found
// ...
static int j;
};
A similar solution would be to apply another existing rule, currently used only in templates, that if T could be a type or something else, then it’s something else; and we can use “typename” if we really mean a type:
struct S {
int i(x); // unabmiguously a data member
int j(typename y); // unabmiguously a member function
};
Both of those solutions introduce subtleties that are likely to be misunderstood by many users (as evidenced by the many questions on comp.lang.c++ about why “int i();” at block scope doesn’t declare a default-initialized int).
The solution proposed in this paper is to allow only initializers of the “= initializer-clause” and “{ initializer-list }” forms. That solves the ambiguity problem in most cases, for example:
HashingFunction hash_algorithm{"MD5"};
Here, we could not use the = form because HasningFunction’s constructor is explicit. In especially tricky cases, a type might have to be mentioned twice. Consider:
vector<int> x = 3; // error: the constructor taking an int is explicit
vector<int> x(3); // three elements default-initialized
vector<int> x{3}; // one element with the value 3
In that case, we have to chose between the two alternatives by using the appropriate notation:
vector<int> x = vector<int>(3); // rather than vector<int> x(3);
vector<int> x{3}; // one element with the value 3
Problem 2:
Another issue is that, because we propose no change to the rules for initializing static data members, adding the static keyword could make a well-formed initializer ill-formed:
struct S {
const int i = f(); // well-formed with forward lookup
static const int j = f(); // always ill-formed for statics
// ...
constexpr static int f() { return 0; }
};
Problem 3:
A third issue is that class-scope lookup could turn a compile-time error into a run-time error:
struct S {
int i = j; // ill-formed without forward lookup, undefined behavior with
int j = 3;
};
(Unless caught by the compiler, i might be intialized with the undefined value of j.)
The proposal:
CWG had a 6-to-3 straw poll in Kona in favor of class-scope lookup; and that is what this paper proposes, with initializers for non-static data members limited to the “= initializer-clause” and “{ initializer-list }” forms.
We believe:
Problem 1: This problem does not occur as we don’t propose the () notation. The = and {} initializer notations do not suffer from this problem.
Problem 2: adding the static keyword makes a number of differences, this being the least of them.
Problem 3: this is not a new problem, but is the same order-of-initialization problem that already exists with constructor initializers.
关于c++ - 为什么成员初始值设定项不能使用括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58536744/
我有一个 MvvmCross MvxSpinner在 Android 中绑定(bind)。用户选择一个值并反射(reflect)在我的属性 MealTypeSelected . 微调器允许用户选择膳
我有一个带有某种类型函数指针的公共(public)变量的表单,如下所示: {...} interface type TExternalFunction = function(var x : TAn
this question中的答案对我理解如何在 IBAction 方法中检索步进器值很有帮助(我投了赞成票),但我仍然不理解在我第一次加载 View 时如何获取步进器的初始值。 我想将初始值设置为
Apple 文档指出: The willSet and didSet observers of superclass properties are called when a property is
我正在将 ColdFusion 应用程序转换为 C#(我是 CF n00b)。 我有一个脚本执行 cfquery,然后执行 cfloop 的结果,它似乎试图将当前行与其下一行进行比较。它似乎试图确保它
我在为 antd 动态表单设置初始值时遇到问题。有什么方法可以在动态中初始化值吗?它需要使用 getFieldDecorator 注册字段。但对于动态字段,该字段之前没有注册。 我收到这个错误:不能在
是否可以动态地为 forms.FileField() 赋予初始值? 最佳答案 我不确定,如果这就是你想要的,但是…… 显然,您不能为文件输入设置初始数据(这意味着您将文件发送给用户)。 Django
我对 Django 表单中 DateTimeField 的初始值没有什么问题。 我在 forms.py 中有声明 class FaultForm(forms.ModelForm): ...
如何使用代码设置 slider 初始值?我知道如何在属性检查器中执行此操作。我想在我的应用程序启动时使用“UserDefault”作为初始值,当我的应用程序第一次启动时,我想将初始值设置为“1.0”
我有一个 RadioButton其 IsChecked 的元素属性绑定(bind)到 MyProperty在 ViewModel . Binding有模式OneWayToSource由于某些原因,它会
我有一个ViewModel类,看起来像这样: class EditUserViewModel( private val initUser: User, ) : ViewModel() {
有人可以帮我解决我的问题吗,问题是我希望我输入到文本字段的第一个初始值只是从 1 到 9 的数字,我正在使用小数垫,所以我也不希望我的初始值是小数点,但在第一个值(仅从 1 t0 9 开始的数字)之后
我正在使用 NSFetchedResultsController 从 CoreData 获取数据并加载包含四个部分的 TableView 。当应用程序第一次运行时,它可以完美地工作,将 plist 加
使用 primeNg 下拉组件,我试图用初始值初始化下拉列表,但没有成功,我使用的是响应式(Reactive)方法。 我检查了 primeNg 文档和演示 - 几乎所有示例都使用模板驱动,我希望模型驱
如何在 ASP.NET 中设置数据绑定(bind)下拉列表的初始值? 例如,我想要这些值,但要显示的第一个值应该是 -- Select One ---,且值为空。 最佳答案 我想你想做的是这样的:
我知道我不能给 BehaviorSubject一个 Observable 值,但我需要一种方法来解决这个问题。在应用程序初始化时,我正在获取当前用户(如果存在),我需要提供 BehaviorSubje
这是我在这里的第一篇文章,所以我希望它是全面的。 我正在使用 AngularJs,并且我使用 Angular 指令添加了一个 JqueryUI slider 。我找到了很多关于如何做到这一点的例子,但
如何有效地为大型数组分配一个公共(public)初始值?例如,如果我有一个 100 x 100 x 100 的整数数组,其中所有初始值都应为零。 在 matlab 中我会简单地写: array = z
我正在为 iOS 应用程序创建一个 Pebble 配套应用程序。我已经使用一些初始值设置了我的 AppSync: Tuplet initial_values[] = { TupletC
有件事我迟到了: const [object, setObject] = useState(new SomeObject()); 在这里,我们在每次重新渲染上构造一个SomeObject实例。然后,如
我是一名优秀的程序员,十分优秀!