C++ default initialization and value initialization: which is which, which is called when and how to reliably initialize a template-type member

Not so hard: A x; A * p = new A; These two are default initialization. Since you don’t have a user-defined constructor, this just means that all members are default-initialized. Default-initializing a fundamental type like int means “no initialization”. Next: A * p = new A(); This is value initialization. (I don’t think there … Read more

How to make a list as the default value for a dictionary? [duplicate]

The best method is to use collections.defaultdict with a list default: from collections import defaultdict dct = defaultdict(list) Then just use: dct[key].append(some_value) and the dictionary will create a new list for you if the key is not yet in the mapping. collections.defaultdict is a subclass of dict and otherwise behaves just like a normal dict … Read more

How can I conditionally provide a default reference without performing unnecessary computation when it isn’t used?

You don’t have to create the default vector if you don’t use it. You just have to ensure the declaration is done outside the if block. fn accept(input: &Vec<String>) { let def; let vec = if input.is_empty() { def = vec![“empty”.to_string()]; &def } else { input }; // … do something with `vec` } Note … Read more

Returning a default value. (C#)

You are looking for the default keyword. For example, in the example you gave, you want something like: class MyEmptyDictionary<K, V> : IDictionary<K, V> { bool IDictionary<K, V>.TryGetValue (K key, out V value) { value = default(V); return false; } …. }