Consider the following code, of a simple class with a constructor taking an argument with a default value.
// Version 1
template <class T>
struct object1 {
using type = T;
constexpr object1(const type& val = type()): value(val) {}
type value;
};
// Version 2
template <class T>
struct object2 {
using type = T;
constexpr object2(const type& val = {}): value(val) {}
type value;
};
// Main
int main(int argc, char* argv[]) {
using type = /* Something */;
object1<type> x1;
object2<type> x2;
auto value1 = x1.value;
auto value2 = x2.value;
// Is there certain types for which value1 and value2 will be different?
return 0;
}
Are the two versions of the constructor equivalent (will always produce the same result for any T
), or they are different?
If they are different, could you provide an example of T
for which the two would lead to different results?
Answer: d) is a bad idea because the default [] will accumulate data and the default [] will change with subsequent calls.
Return Type: Returns an empty list if no parameters are passed. If a parameter is passed then it returns a list of elements in the iterable.
Default arguments are overwritten when the calling function provides values for them. For example, calling the function sum(10, 15, 25, 30) overwrites the values of z and w to 25 and 30 respectively.
My Python Blog | Avoid using an empty list as a default argument to a function A very common error in Python is the use of an empty list as a default argument to a function. This is not the right way to do it and can cause unwanted behavior.
The effects of default initialization are: if T is a non-POD (until C++11) class type, the constructors are considered and subjected to overload resolution against the empty argument list. if T is an array type, every element of the array is default-initialized;
Scalars and POD types with dynamic storage duration were considered to be not initialized (since C++11, this situation was reclassified as a form of default initialization). each direct non-static data member M of T is of class type X (or array thereof), X is const-default-constructible, and
List initialization is performed in the following situations: direct-list-initialization (both explicit and non-explicit constructors are considered) 1) initialization of a named variable with a braced-init-list (that is, a possibly empty brace-enclosed list of expressions or nested braced-init-lists)
No, they are not equivalent. Second variant relies on implicitness of T's default constructor:
class foo
{
public: explicit foo() {}
};
object1<foo> of{}; // ok
object2<foo> of{}; // error
Also I think it is not a good idea to call a copy constructor from a temporary instead of calling a default constructor without a temporary. That is it would be better to implement separate constructors:
template <class T>
struct object1 {
using type = T;
constexpr object1(void): value{} {}
constexpr object1(const type& val): value{val} {}
type value;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With