I don’t think I would be able to dispel all your doubts, opisop, because, for what I can see, our approach to study is a bit different: you appear to appreciate delving into the details of theory, while I prefer to know the jist of the things in practice. Plus, my English is not rich enough to let me discuss all the ins and outs of anything. But, if you surf internet by the keywords “c++ uniform initialization” or “c++ uniform initializer”, you can easily find many good articles about it.
But, having read your examples, let me ask you if you remember the differences in C++ between declaration, definition and initialization - otherwise, you’d better brush up about them before going on.
Please note that syntax has been called “uniform initializer” for a good reason: it’s used to
initialize things. For example, it has (normally) nothing to do with function paramethers.
Let’s look at your examples:
1 2
|
// Assignment
int a = 5;
|
That’s not usually called assignment, but initialization. This is usually called assignment:
1 2
|
int a = 5; // 'a' is declared, defined and initialized (to '5')
a = 4; // 'a' is assigned '4'
|
(Well, yes, maybe people, me the first, in ‘normal’ speaking/writing don’t pay all that attention to such details, so those terms are often mixed up)
In such a case you can use {}, which is usually safer:
1 2
|
// becomes
int a {5}; // correct
|
1 2 3 4
|
// Equation
b = cos(j) / 12.0 * f(y, z);
// becomes
b {cos({j}) / 12.0 * f({y}, {z})};
|
What is 'b'? Is it an int? We can’t know, we can only guess, because it has already been declared before. What are you initializing here, then? Here you are assigning. So, no, you need to use ‘=’.
1 2 3 4
|
// New Class
MyClass class1(1, 'a', 3.0);
// becomes
MyClass class1({1}, {'a'}, {3.0}}
|
Apart from the syntax error, these two codes make exactly the same thing, since the braces around any single element don’t change anything.
Anyway, maybe you want to re-read about std::initializer_list and member initializer list:
https://en.cppreference.com/w/cpp/utility/initializer_list
https://en.cppreference.com/w/cpp/language/initializer_list