Search code examples
c++initializationinitializer-list

What is narrowing cast when I use initializer list?


One guy tell me that more effective to write

SomeType val{another_val};

than

SomeType val = another_val;

cause in the second case we have narrowing cast.

Can you explain what is this? And is this true that initialize with initializer list more effective?


Solution

  • Using braces is another way of making your code safer, that's all. For example

    int main() {
        unsigned n = -1.0; // hopefully a compiler warning, undefined behaviour
        unsigned m {-1.0}; // certainly a compiler diagnostic
    }