[Spice-devel] Coding style and naming conventions for C++

Lukáš Hrázký lhrazky at redhat.com
Tue Jan 30 13:13:24 UTC 2018


On Tue, 2018-01-30 at 07:09 -0500, Frediano Ziglio wrote:
...

> > > 3. Namespace coding style
> > > 
> > > a) Let's not use `using namespace ...` ever even in .cpp files
> > > (see
> > > i.e. [1]). In streaming agent we have at the beginning of every
> > > .cpp:
> > > 
> > > using namespace std;
> > > using namespace SpiceStreamingAgent;
> > 
> > Again, 2000 lines of code, unlikely to grow much.
> > Google’s rule applies to their mega-projects, but for the agent,
> > I think that “using namespace” makes the code leaner.
> > 
> 
> I think here the distinction is usage and implementation.
> 
> 1) implementation. If you want to implement a class
> my_namespace::MyClass you
>   probably want to use:
> 
> #include "my_class_header.hpp"
> 
> namespace my_namespace {
> 
> MyClass::MyClass(...)
> {
>   ...
> }
> 
> ...
> 
> }

Yes, that's how I meant it.

> 2) usage. Here you want to use the class my_namespace::MyClass, you
> probably
>   want:
> 
> #include "my_class_header.hpp"
> 
> using namespace my_namespace;
> 
> ...
> 
>    auto *my_obj = new MyClass(...);
> 
> 
> 
> I think does it make sense. About the using in 2) depends on how much
> the code
> is using the namespace or personal preference.

Yes, you can do that. You can also do (although you shouldn't really
use raw pointers in C++ ;)):

namespace mn = my_namespace;

...

    auto *my_obj = new mn::MyClass(...);


Which I think is considered better practice. Nobody likes long
identifiers :)

> > Let’s first share our preference on existing styles to see if we
> > agree on
> > anything…
> > As for me, I have a slight preference for the LLVM coding style,
> > but I made
> > modifications in my own clang-format files.
> > 
> 
> The problem here is that we already have plenty of other code, not
> clear
> if/how we should be coherent with it (considering also that's almost
> C but
> considering that and the fact that C++ is really C friendly I can
> think
> that we could include and use lot of existing C code).

Agreed, as mentioned in my reply to Christophe.

Lukas

> > Regards
> > Christophe
> > 
> > 
> > > 
> > > Lukas
> > > 
> > > 
> > > [1] https://google.github.io/styleguide/cppguide.html#Namespaces
> 
> Frediano


More information about the Spice-devel mailing list