When to use explicit constructors (C ++ 11)

I read about using Explicit in constructors and some of its possible results. There are many messages available when stack overflows, but my main problem is that I need some specific points that need to be considered when designing a constructor and deciding on its convertibility. From my current research:

  • I believe explicit constructors are more verbose. This one has both pros (no implicit conversions, more readable code), and minuses (without converting constructors as return values, more for writing).
  • In addition, with explicit constructors, it is not possible to initialize copying. In this case, I cannot understand all the results of this behavior, given that I am relatively new to C ++. Copying constructors may be an exception.
  • The context of using the constructed class also affects the decision explicitly.

So my question is:

  • Are there any other key points or rules (at least for beginners) to keep in mind when deciding on explicit use in C ++ 11?
  • Although the general rule for creating explicit constructors with one argument is not appropriate (implicit conversion to a constructor with multiple arguments with C ++ 11 list initializers), should it be followed?
+4
source share

Source: https://habr.com/ru/post/1668577/


All Articles