inline namespace technique for managing platform specific code in c++

372 Views Asked by At

I have seen usage of #ifdef macros ( example Eigen library ) to manage platform specific, but haven't seen any one use "inline namespace"s to manage platform specific code.

The github repo belows gives specific code and example usage. https://github.com/dchichkov/curious-namespace-trick/wiki/Curious-Namespace-Trick

I am wondering if it is a viable technique to use or if there are any gotchas that I am not able to see. Below is the code snippet :

#include <stdio.h> 

namespace project { 
  // arm/math.h 
  namespace arm { 
    inline void add_() {printf("arm add\n");}  // try comment out 
  } 

  // math.h 
  inline void add_() { 
    // 
    printf("common add\n"); 
    // 
  } inline namespace platform {inline void add() {add_();}} 


  inline void dot_() { 
    // 
    add(); 
    // 
  } inline namespace platform {inline void dot() {dot_();}} 
} 

int main() { 
 project::dot(); 
 return 1; 
} 

Output :

$g++ func.cpp -Dplatform=common ; ./a.out common add

$ g++ func.cpp -Dplatform=arm ; ./a.out arm add

2

There are 2 best solutions below

0
On

There are at least two ways to achieve same results. First one with namespaces. Second one - with static functions in classes.

With namespaces:

#include <stdio.h>

namespace GenericMath
{
    void add();
    void dot();
}

namespace ArmMath
{
    void add();

    using GenericMath::dot;
}

namespace GenericMath
{
    void add() 
    {
        printf("generic add");
    }

    void dot() 
    {
        Math::add();
        printf("generic dot");
    }
}

namespace ArmMath
{
    void add() 
    {
        printf("arm add");
    }

    using GenericMath::dot;
}

int main()
{
    Math::dot();
    return 1;
}

With classes:

#include <stdio.h>

class GenericMath
{
public:
    static void add();
    static void dot();
};

class ArmMath : public GenericMath
{
public:
    static void add();
};

void GenericMath::add() 
{
    printf("generic add");
}

void GenericMath::dot() 
{
  printf("generic dot");
  Math::add();
}

void ArmMath::add() 
{
  printf("arm add");
}

int main()
{
    Math::add();
    Math::dot();
    return 1;
}

IMO inline namespaces make code less readable and too verbose.

4
On

Once issue is that unlike #ifdef, the compiler still compiles all the code that isn't for the current platform.

So you can't use it for dealing with platform-specific APIs.