Skip to content

Commit

Permalink
CPP-5374 S1709 Introduce exception for "std::initializer_list" constr…
Browse files Browse the repository at this point in the history
…uctor
  • Loading branch information
tomasz-kaminski-sonarsource authored Jun 6, 2024
1 parent 01b801d commit 0e09fb7
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions rules/S1709/cfamily/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ int test(Bar& bar, Baz& baz) {

=== Exceptions

The issue is not raised for constructors that have a single parameter of type `std::initializer_list` -
such constructors have special meaning and allow objects to be constructed from brace delimited list of initializers.

[source,cpp]
----
struct Container {
Container(std::initializer_list<int> elems); // Compliant
};
void handle(Container const& c);
int test(Bar& bar, Baz& baz) {
Container c1{1, 2, 3}; // OK whether the constructor is explicit or not
Container c2 = {1, 2, 3}; // Ill-formed if constructor would be explicit
handle({1, 2, 3}); // Ill-formed if constructor would be explicit
handle(Container{1, 2, 3}); // OK whether the constructor is explicit or not
}
----


{cpp}20 introduced conditional `explicit(expr)` that allows developers to make a constructor or conversion operator conditionally explicit depending on the value of `expr`.
The new syntax allows a constructor or conversion operator declared with an `explicit(expr)` specifier to be implicit when `expr` evaluates to `false`.
The issue is not raised in such situation.
Expand Down

0 comments on commit 0e09fb7

Please sign in to comment.