initialization

SwiftUI View (apparently) laid out before init runs

好久不见. 提交于 2020-05-17 05:54:28
问题 TL;DR It seems that the ContentView below evaluates the body's if statement before init has run. Is there a race condition, or is my mental model out-of-order? Kudos A shout-out to Asperi, who provided the state-initializer equivalent that solves today's problem. Code Why does ContentView display "dummy is nil"? It seems something gets closed over before the initializer sets dummy . What is it about the second assignment that fixes things? class Dummy { } struct ContentView: View { @State

How can I avoid most vexing parse with direct value initialization

限于喜欢 提交于 2020-05-15 06:49:26
问题 After read Most vexing parse, I understand the following code is also ambigous T x(); In one hand, it can be interpreted as a function declaration which returns an object of T , on the other hand, it can also be interpreted as a variable definition, and object x is value-initialized. I understand I can use uniform initialization like the following code to avoid confliction T x{}; I also understand if T is a (non-POD before C++11) class and the following default initialization actually equals

Why negative size of array is not a compilation error but throws java.lang.NegativeArraySizeException

落花浮王杯 提交于 2020-05-14 22:06:24
问题 java does not allow to initialize an array with negative size. For example: int[] arr = new int[-1]; If this is already known, why it throws NegativeArraySizeException instead of compilation error? Just curious to know why java decides it to be thrown at runtime while it is known at compile time that it will fail. 回答1: This check could be performed at compile time only in situations when the size is specified as a constant expression. However, Java Language Specification requires this check

Is this an incorrect warning?

喜欢而已 提交于 2020-05-12 02:29:30
问题 Let's see this code pattern I'm seeing often: struct Foo { template <typename T> T* as1() { /* ... */ } template <typename T> T* as2(T*) { /* ... */ } }; The former method is to be used like this: SomeComplexTypeAndNotAuto * a = foo.as1<SomeComplexTypeAndNotAuto>(); While the latter is more convenient to use since you don't need to repeat the complex type: SomeComplexTypeAndNotAuto * a = foo.as2(a); However, most compiler rejects the 2nd case with a Wuninitialized warning: warning: variable

Is this an incorrect warning?

别来无恙 提交于 2020-05-12 02:26:22
问题 Let's see this code pattern I'm seeing often: struct Foo { template <typename T> T* as1() { /* ... */ } template <typename T> T* as2(T*) { /* ... */ } }; The former method is to be used like this: SomeComplexTypeAndNotAuto * a = foo.as1<SomeComplexTypeAndNotAuto>(); While the latter is more convenient to use since you don't need to repeat the complex type: SomeComplexTypeAndNotAuto * a = foo.as2(a); However, most compiler rejects the 2nd case with a Wuninitialized warning: warning: variable

Why doesn't initializing a C++ struct to `= {0}` set all of its members to 0?

好久不见. 提交于 2020-05-11 10:56:27
问题 After doing a ton of testing and writing this answer (note: the downvote on it was BEFORE my total rewrite of it), I can't understand why = {0} doesn't set all members of the struct to zero! If you do this: struct data_t { int num1 = 100; int num2 = -100; int num3; int num4 = 150; }; data_t d3 = {0}; printf("d3.num1 = %i\nd3.num2 = %i\nd3.num3 = %i\nd3.num4 = %i\n\n", d3.num1, d3.num2, d3.num3, d3.num4); ...the output is: d3.num1 = 0 d3.num2 = -100 d3.num3 = 0 d3.num4 = 150 ...although I

Initialization with empty curly braces

一笑奈何 提交于 2020-05-11 07:32:07
问题 First attempt and everything works fine: class Base { public: Base() {std::cout << "default ctor!\n"; } }; ... Base b{}; Base b_one = {}; Another way of implementation(add explicit ): class Base { public: explicit Base() {std::cout << "default ctor!\n"; } }; ... Base b{}; Base b_one = {}; // error! Why? I have read on cppreference that in both cases default initialization would be used and no diffences. From list initialization: Otherwise, If the braced-init-list is empty and T is a class

How to iterate through a Map in java?

本小妞迷上赌 提交于 2020-05-09 18:45:30
问题 I need to iterate through a BucketMap and get all keys but how do I get to something like buckets[i].next.next.next.key for instance without doing it manually as I tried here: public String[] getAllKeys() { int j = 0; //index of string array "allkeys" String allkeys[] = new String[8]; for(int i = 0; i < buckets.length; i++) { //iterates through the bucketmap if(buckets[i] != null) { //checks wether bucket has a key and value allkeys[j] = buckets[i].key; //adds key to allkeys j++; // counts up

How to initialize a struct to 0 in C++

…衆ロ難τιáo~ 提交于 2020-05-05 12:34:19
问题 Here is a related C answer that doesn't work (as a zero initializer for a struct) on C++: Initializing a struct to 0. One of the solutions presented is this: myStruct _m1 = {0}; This works fine in C, but it doesn't work in C++. :( : error: cannot initialize a member subobject of type 'myScope::MyStruct' with an rvalue of type 'int'`. How do you zero-initialize a struct in C++? Related: Initializing a struct to 0 in C: Initializing a struct to 0 Update: (an adjacent, but NOT duplicate question

C++ CRTP initialization

那年仲夏 提交于 2020-04-28 20:52:59
问题 i ran into a segfault running the following program #include <iostream> #include <vector> template <typename Derived> struct CRTPBase { CRTPBase() { func(); } void func() { static_cast<Derived*>(this)->_func(); } }; struct CRTPChild : CRTPBase<CRTPChild>{ using CRTPBase<CRTPChild>::CRTPBase; void _func(){ vec.resize(10); vec[0] = 2; } std::vector<int> vec; }; int main() { CRTPChild obj; std::cout << obj.vec[0] << std::endl; } When i replace vec with a member of type int it doesn't segfault