简体   繁体   中英

Is it bad code to have internal functions for namespaces

Say I have a namespace Util, which contains some basic functions. But in that namespace I need the functionality of a library to do my work, however I don't want the header file to know about it so I end up making an anonymous namespace with the functionality that I need but don't want exposed. Is there a better way to do this ?

As an example:

# Util.h  
namespace Util{  int Add();  }

# Util.cpp
namespace Util{
 namespace {
  funkyInt Add_internal(int x, int y);
  int convert(funkyInt x);
 }

 int Add(int x, int y){
  return convert(Add_internal(x,y))
 }

 funkyInt Add_internal(int x, int y){
 //DOSomething
 }
}

That's perfectly fine.

Whether the anonymous namespace is in your namespace or the global one is taste and ease.

  • Taste, because the code may be exactly identical
  • Ease because if you reuse other function of your non anonymous namespace, you don't have to prefix or use the namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM