Last active
May 16, 2017 20:37
-
-
Save pyrrho/61d7625b0400095742c370a0b0284c60 to your computer and use it in GitHub Desktop.
C++14 inline Variable Hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "a.h" | |
#include "shared.h" | |
namespace A { | |
int & getShared() { return shared_int; } | |
int * getSharedAddr() { return &shared_int; }; | |
int & getMacro() { return macro_int; } | |
int * getMacroAddr() { return ¯o_int; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace A { | |
int & getShared(); | |
int * getSharedAddr(); | |
int & getMacro(); | |
int * getMacroAddr(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "b.h" | |
#include "shared.h" | |
namespace B { | |
int & getShared() { return shared_int; } | |
int * getSharedAddr() { return &shared_int; }; | |
int & getMacro() { return macro_int; } | |
int * getMacroAddr() { return ¯o_int; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace B { | |
int & getShared(); | |
int * getSharedAddr(); | |
int & getMacro(); | |
int * getMacroAddr(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include "a.h" | |
#include "b.h" | |
int main () { | |
printf("A::getShared() : %d\n", A::getShared()); // 4 | |
printf("A::getSharedAddr() : %p\n", A::getSharedAddr()); // 0x106809288 | |
printf("A::getMacro() : %d\n", A::getMacro()); // 42 | |
printf("A::getMacroAddr() : %p\n", A::getMacroAddr()); // 0x1068092b8 | |
printf("B::getShared() : %d\n", B::getShared()); // 4 | |
printf("B::getSharedAddr() : %p\n", B::getSharedAddr()); // 0x106809288 | |
printf("B::getMacro() : %d\n", B::getMacro()); // 42 | |
printf("B::getMacroAddr() : %p\n", B::getMacroAddr()); // 0x1068092b8 | |
printf("\n"); // | |
printf("A::getMacro() : %d\n", ++A::getMacro()); // 43 | |
printf("B::getMacro() : %d\n", ++B::getMacro()); // 44 | |
printf("A::getMacro() : %d\n", ++A::getMacro()); // 45 | |
printf("B::getMacro() : %d\n", ++B::getMacro()); // 46 | |
printf("A::getMacro() : %d\n", ++A::getMacro()); // 47 | |
printf("B::getMacro() : %d\n", ++B::getMacro()); // 48 | |
} | |
// To build | |
// clang++ -std=gnu++14 -c a.cpp -o a.o | |
// clang++ -std=gnu++14 -c b.cpp -o b.o | |
// clang++ -std=gnu++14 a.o b.o main.cpp -o main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment