I have a single global helper function which is used by a bunch of macros in a header file. The intention is to allow the macros to be usable by simply #include'ing the single header (in other words, I'd like to keep the function definition in the header and avoid putting it in a separate compilation unit). However, this causes problems when an application #include's this file in more than one compilation unit, as the duplicate symbols problem arises.
The helper function has enough characteristics in which it shouldn't be declared inline.
I was messing around and found that using unnamed namespaces solves the problem of duplicate symbols, i.e.:
namespace
{
void foo(...)
{
}
};
#define HELPER_A foo(1, ...);
#define HELPER_B foo(2, ...);
...
Is there any downside to this approach? Are there better alternatives?
I'd like to keep the function definition in the header and avoid putting it in a separate compilation unitWhy?The helper function has enough characteristics in which it shouldn't be declared inlineI doubt it. If the compiler doesn't think the function should be inlined then it won't be, but theinlinekeyword will give the function the correct linkage to avoid the multiple definition error.