17

My buddy and I have been recently reading leveldb source code. And we encounter this problem. In leveldb db/skiplist.h file, there is a constructor declaration:

explicit SkipList(Comparator cmp, Arena* arena);

I know explicit constructor with single parameter means no implicit type conversion for constructor parameter. But what does double parameters constructor with explicit keyword mean? Is it new rule of C++11?

Thanks.

5
  • Is this the exact declaration? Has arena no default value? Commented Jul 15, 2013 at 6:55
  • 2
    @Gorpik I dont know if the OP has exactly the same interface but look here. Commented Jul 15, 2013 at 6:59
  • The C++ "explicit" keyword is used to avoid implicit cast Commented Jul 15, 2013 at 7:02
  • @TobiMcNamobi Thanks for the link. Line 46 is what I quote. Commented Jul 15, 2013 at 7:03
  • @alexbuisson yes, as I mentions in my question. But what about mutiple parameter constructor? Commented Jul 15, 2013 at 7:06

1 Answer 1

16

With C++11, you can use braced-init-lists in place of some other expressions, and that makes a difference. For instance, you can use them in return statements:

SkipList foo() {
    return {{}, nullptr}; //does not compile with explicit constructor
    return SkipList{{}, nullptr}; //compiles with or without explicit constructor
}
Sign up to request clarification or add additional context in comments.

6 Comments

By uniform initialization You mean initializing multiple parameter like initializing an array?
@lulyon, Uniform initialization generally results from using braces. You can use them to call constructors, as shorthand for TypeName(), and to eliminate the most vexing parse. It's actually quite a significant feature of C++11. You should look up some information on it.
This is called list initialization
@M.M, Thanks, I changed it to use braced-init-list instead to match the "top level" of wording, even though it is list-initialized when all is said and done.
I wouldn't be surprised if this constructor originally had a default argument (or was adapted from a single-argument constructor). There's not that many cases where you'd actually want to disable copy-list-initialization.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.