Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make initializer list implicitly convert to the class?

For example, I have a class

struct A
{
    A(int i, double d) {...}

private:
    int m_i;
    double m_d; 
};

and a function with an argument A

void f(A a);

And I can use initializer list to call the function

f( A{1, 3.14} );

How to make the following simple version also works?

f( {1, 3.14} );
like image 504
user1899020 Avatar asked Jun 24 '14 18:06

user1899020


1 Answers

The call of the function with the initializer list will work. You should do nothing special.:)

The call would not be compiled if the constructor had function specifier explicit. In this case you have to use the previous call of the function

f( A{1, 3.14} );

using functional notation of casting the initializer list to an object of type A.

like image 132
Vlad from Moscow Avatar answered Nov 01 '22 13:11

Vlad from Moscow