Built-in namespace method for managing specific platform code in C ++

I saw the use of #ifdef macros (such as the Eigen library) to control a specific platform, but I did not see anyone using the "built-in namespace" to control a specific platform code.

The github repo sheets provide specific code and usage examples. https://github.com/dchichkov/curious-namespace-trick/wiki/Curious-Namespace-Trick

I am wondering if it is possible to use a viable technique or if there are any errors that I cannot see. The following is a snippet of code:

#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; 
} 

Exit:

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

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

+4
2

. . - .

:

#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;
}

:

#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 .

0

, #ifdef, , .

API-, .

0

Source: https://habr.com/ru/post/1625425/


All Articles