[webkit-dev] "using" statement clarifications

Chris Jerdonek chris.jerdonek at gmail.com
Sat Dec 5 09:29:08 PST 2009


This e-mail has two related questions.

I noticed that the style-check script enforces a rule that isn't
stated in the coding style guidelines.  I'd like to clarify this rule
before adding it to the guidelines.

The rule is that "using std::foo" statements should be replaced by
"using namespace std" in files not ending in .c or .m.  The
"check_using_std" function of cpp_style.py enforces this rule:

http://trac.webkit.org/browser/trunk/WebKitTools/Scripts/modules/cpp_style.py

I've also noticed, though, that some existing _header_ files have
"using std::foo" statements (where "foo" is swap, pair, make_pair,
min, or max).

(Q1) What was the reason for this rule, and for consistency, which of
the following should hold?

(A) The "using std" rule above should not apply to header files.
(B) The "using std" rule above should apply to header files, in which
case we also need to clarify an existing style guideline as described
below.
(C) Statements of the form "using std::foo" should not be allowed in
header files at all -- even when not in global or "namespace" scope.
(They are already not allowed in header files in global or namespace
scope.)

The following related, existing guideline also seems to need
clarification (currently rule #4 in the "'using' statements" section):

> Any other "using namespace" statements [i.e. statements for non-nested
> namespaces or for nested namespaces whose parent namespace is not
> defined in the file] must be declared before the first namespace
> definition in the file.

It seems like this rule shouldn't apply to "using namespace"
statements in block or class scope, since this would imply that "using
namespace" statements aren't allowed in block or class scope.  At the
least, it seems like it shouldn't apply to header files since we don't
allow header files to have "using" statements in global scope to begin
with.

(Q2) What is the reason for the "using namespace" rule above, and to
clarify, which of the following should hold?

(L) The "using namespace" rule above should apply only to statements
already in global or namespace scope.
(M) The "using namespace" rule above should apply to all "using
namespace" statements (in particular, "using namespace" statements are
not allowed in block or class scope).  However, the rule should apply
only to implementation files (since we do not allow header files to
have "using" statements in global scope).
(N) The "using namespace" rule above should apply to all "using
namespace" statements.  In particular, "using namespace" statements
are not allowed in block or class scope, and header files cannot have
"using namespace" statements in any scope.

I'm listing (N) for completeness, even though at least one person has
already remarked that it's okay to use "using namespace" statements in
header files in non-global scopes:

https://lists.webkit.org/pipermail/webkit-dev/2009-November/010518.html

I will add clarifying information to the web site after I hear back.

Thanks,
--Chris


More information about the webkit-dev mailing list