123

Does making a constructor having multiple arguments explicit have any (useful) effect?

Example:

class A {
    public:
        explicit A( int b, int c ); // does explicit have any (useful) effect?
};
0

4 Answers 4

156

Up until C++11, yeah, no reason to use explicit on a multi-arg constructor.

That changes in C++11, because of initializer lists. Basically, copy-initialization (but not direct initialization) with an initializer list requires that the constructor not be marked explicit.

Example:

struct Foo { Foo(int, int); };
struct Bar { explicit Bar(int, int); };

Foo f1(1, 1); // ok
Foo f2 {1, 1}; // ok
Foo f3 = {1, 1}; // ok

Bar b1(1, 1); // ok
Bar b2 {1, 1}; // ok
Bar b3 = {1, 1}; // NOT OKAY
2
  • 10
    I think this answer would be better with "Why would I want that" or "When is this usefull" explanation.
    – MateuszL
    Commented May 10, 2019 at 5:53
  • @MateuszL Edgar's answer gives probably the best argument for why it might be useful (and arguably deserves the tick). The reason it's there, though, is simply because it's the logical extension of the existing semantics for explicit. I would not personally bother making multi-arg constructors explicit.
    – Sneftel
    Commented Jun 8, 2020 at 16:25
38

You'd stumble upon it for brace initialization (for instance in arrays)

struct A {
        explicit A( int b, int c ) {}
};

struct B {
         B( int b, int c ) {}
};

int main() {
    B b[] = {{1,2}, {3,5}}; // OK

    A a1[] = {A{1,2}, A{3,4}}; // OK

    A a2[] = {{1,2}, {3,4}}; // Error

    return 0;
}
28

The excellent answers by @StoryTeller and @Sneftel are the main reason. However, IMHO, this makes sense (at least I do it), as part of future proofing later changes to the code. Consider your example:

class A {
    public:
        explicit A( int b, int c ); 
};

This code doesn't directly benefit from explicit.

Some time later, you decide to add a default value for c, so it becomes this:

class A {
    public:
        A( int b, int c=0 ); 
};

When doing this, you're focussing on the c parameter - in retrospect, it should have a default value. You're not necessarily focussing on whether A itself should be implicitly constructed. Unfortunately, this change makes explicit relevant again.

So, in order to convey that a ctor is explicit, it might pay to do so when first writing the method.

2
  • 1
    But what about the case when the maintainer adds that default and concludes that the result should be available as a converting constructor? Now they have to remove that explicit that's been there forever, and tech support will be inundated with calls about that change and spend hours explaining that explicit was just noise, and that removing it is harmless. Personally, I'm not very good at predicting the future; it's hard enough deciding what an interface should look like now. Commented Aug 24, 2016 at 15:00
  • @PeteBecker That's a good point. I personally think that the two cases are asymmetric, and that it's much more common when making parameters default (or removing them) to inadvertently make the class implicitly constructible, then to actually realize at the same time that in retrospect it should have be so. That being said, these are "soft" considerations, and might vary between people/projects/etc., or even just be a matter of taste.
    – Ami Tavory
    Commented Aug 24, 2016 at 15:08
15

Here's my five cents to this discussion:

struct Foo {
    Foo(int, double) {}
};

struct Bar {
    explicit Bar(int, double) {}
};

void foo(const Foo&) {}
void bar(const Bar&) {}

int main(int argc, char * argv[]) {
    foo({ 42, 42.42 }); // valid
    bar({ 42, 42.42 }); // invalid
    return 0;
}

As you can easily see, explicit prevents from using initializer list alongside with bar function bacause the constructor of struct Bar is declared as explicit.

1
  • I typically declare multi-parameter constructors explicit, for this reason - forces the use of the class name at the function call location. Commented Mar 10, 2022 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.