Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default argument and empty list initialization

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?

like image 982
Vincent Avatar asked Aug 15 '18 09:08

Vincent


People also ask

Why shouldn't you make the default arguments an empty list?

Answer: d) is a bad idea because the default [] will accumulate data and the default [] will change with subsequent calls.

What function do I use to return a default empty list?

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.

What is default argument explain with example?

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.

Should you avoid using an empty list as a default argument?

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.

What are the effects of Default initialization in C++?

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;

Which types are not initialized by default in C++?

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

What is list initialization in C++?

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)


Video Answer


1 Answers

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;
};
like image 58
user7860670 Avatar answered Oct 24 '22 10:10

user7860670