Question

I work on a C++ project that runs on Windows, Mac and Linux. As a result it's not uncommon to be working on the same part of code in Visual Studio one day, Xcode the next, Qt Creator the next, etc.

Lately I have taken to using the namespace{} keyword in much the same way that #region/#endregion are used in C#. For example:

// MyFile.cpp

namespace MyNamespace
{
  // lots of code related to one class
}

namespace MyNamespace
{
  // lots of code related to another class
}

namespace MyNamespace
{
  // lots of code related to yet another class
}

While this doesn't allow me to name the regions per say, I still find it very helpful being able to collapse large chunks of code. I know that IDE-specific solutions exist, such as #pragma region/#pragma endregion but I was unable to find a way that was IDE agnostic for C++. Also, I know that these large chunks could be put in their own files, but for the purposes of this question please assume that this is not an option.

My question is: does this create any overhead in the compiled binaries? Can doing this impact performance negatively or impact the end result (binary) at all?

(Note: We use Microsoft's compiler on Windows, clang on Mac and gcc on Linux.)

Thank you!

Was it helpful?

Solution

The short answer to your questions are "no". Follow long answers:

does this create any overhead in the compiled binaries?

No. Assuming you were already using the namespaces, the impact is absolutely zero. If you weren't previously using the namespace, there is a small impact in the size of the mangled identifiers to link.

This:

void identifier() {}        
namespace longnamespace { void identifier() {} }

Become the symbols _Z10identifierv and _ZN13longnamespace10identifierv, respectively. If you have a huge project, with hundreds of thousands of symbols, it may affect (albeit slightly) your link time and your dynamic-link (loading) time.

Can doing this impact performance negatively or impact the end result (binary) at all?

Yes, at least in terms of loading times. But not a lot. And there are solutions to mitigate it (google for "prelink" and "prebinding").

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top