Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why double parameter constructor begins with an explicit keyword?

Tags:

c++

explicit

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.

like image 956
lulyon Avatar asked Jul 15 '13 06:07

lulyon


People also ask

Why an explicit constructor was introduced?

The explicit function specifier controls unwanted implicit type conversions. It can only be used in declarations of constructors within a class declaration. For example, except for the default constructor, the constructors in the following class are conversion constructors.

What is implicit constructor in C++?

implicit constructor is a term commonly used to talk about two different concepts in the language, the. implicitly declared constructor which is a default or copy constructor that will be declared for all user classes if no user defined constructor is provided (default) or no copy constructor is provided (copy).


1 Answers

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
}
like image 167
chris Avatar answered Oct 19 '22 18:10

chris