c++ explicit keyword for function without arguments
Possible Duplicate:
What does the explicit keyword in C++ mean?
对于不带任何参数的函数,是否有理由使用
在记录boost::char_分隔符的页面末尾附近(http://www.boost.org/doc/libs/1_47_0/libs/tokenizer/char_separator.htm),但没有进一步解释。
阅读成员说明:
1 2 3 4 | explicit char_separator(const Char* dropped_delims, const Char* kept_delims ="", empty_token_policy empty_tokens = drop_empty_tokens) explicit char_separator() |
第一个构造函数的
第二个构造函数的
编辑
从C++标准看:
7.1.2第6页说明:
The explicit specifier shall be used only in declarations of
constructors within a class declaration; see 12.3.1.
12.3.1 P2说明:
An explicit constructor constructs objects just like non-explicit
constructors, but does so only where direct-initialization syntax
(8.5) or where casts (5.2.9, 5.4) are explicitly used. A default
constructor may be an explicit constructor; such a constructor will be
used to perform default-initialization or value-initialization (8.5).
[Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Z { public: explicit Z(); explicit Z(int); // ... }; Z a; // OK: default-initialization performed Z a1 = 1; // error: no implicit conversion Z a3 = Z(1); // OK: direct initialization syntax used Z a2(1); // OK: direct initialization syntax used Z* p = new Z(1); // OK: direct initialization syntax used Z a4 = (Z)1; // OK: explicit cast used Z a5 = static_cast<Z>(1); // OK: explicit cast used |
—end example]
因此,使用