You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@impala.apache.org by Zachary Amsden <za...@cloudera.com> on 2017/05/03 06:15:51 UTC

Nested namespaces in C++ code

So I've recently come upon a situation where it would be useful to have
nested namespaces.  The reason is that there are a lot of static functions
in a class which I would like to benchmark.  Writing the same code to run
the benchmark test over and over is very tedious and is much more
efficiently accomplished with a macro.  However, since the functions are
embedded as static functions in a class, it isn't possible to pass the
names along as macro parameters, which would make this task much easier.
Instead I have to resort to a lot of duplicated parameters or multiple
levels of macros (since I can't easily convert :: into a legal member of a
function name).

If the functions were embedded instead in a nested namespace, a simple
'using namespace::subspace' declaration would bring all these to top level
in the benchmark and all would be well.

I've seen conflicting documentation about nested namespace in Apache C++
style guides, so wondering if we could have a discussion on that.

Thanks,

 - Zach

Re: Nested namespaces in C++ code

Posted by Jim Apple <jb...@cloudera.com>.
> So I've recently come upon a situation where it would be useful to have
> nested namespaces.

This happens in a couple of places already: git grep -E 'impala::[a-z]' be

> The reason is that there are a lot of static functions
> in a class which I would like to benchmark.  Writing the same code to run
> the benchmark test over and over is very tedious and is much more
> efficiently accomplished with a macro.

Why can't you pass the methods in as parameter or a template parameter?

> However, since the functions are
> embedded as static functions in a class, it isn't possible to pass the
> names along as macro parameters, which would make this task much easier.

I'm sorry, I don't understand. Here is what I wrote trying to
understand, but I think I am missing some big point.

struct Foo {
  static void Bar();
};

#define CALL(f) f()

template<void F()>
void TemplateCall() { F(); }

void ParameterCall(void f()) { f(); }

int main() {
  CALL(Foo::Bar);
  TemplateCall<Foo::Bar>();
  ParameterCall(Foo::Bar);
}

> I've seen conflicting documentation about nested namespace in Apache C++
> style guides, so wondering if we could have a discussion on that.

Each project has its own style guide. Here is Impala's:

https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=65868536