Skip to content

Instantly share code, notes, and snippets.

@reyoung
Created May 6, 2022 10:42
Show Gist options
  • Save reyoung/de009c0532caf2a7551663e6a6c37fe9 to your computer and use it in GitHub Desktop.
Save reyoung/de009c0532caf2a7551663e6a6c37fe9 to your computer and use it in GitHub Desktop.
#include "a.h"
#include <iostream>
static int _inited = []() -> int {
std::cout << "NS: " << NSFoo() << ", Static: " << StaticFoo()
<< ", Inline: " << InlineFoo() << ", Cls: " << Foo::ClsFoo()
<< ", Weak: " << WeakSymbFoo() << std::endl;
return 0;
}();⏎
#pragma once
#include <iostream>
namespace {
int *NSFoo() {
static int val;
return &val;
}
} // namespace
static int *StaticFoo() {
static int val;
return &val;
}
inline int *InlineFoo() {
static int val;
return &val;
}
class Foo {
public:
static int *ClsFoo() {
static int val;
return &val;
}
};
int *WeakSymbFoo() __attribute__((weak));
int *WeakSymbFoo() {
static int val;
return &val;
}⏎
#!/bin/bash
clang++ -std=c++20 *.cpp -o a.out
#include "a.h"
#include <iostream>
static int _inited = []() -> int {
std::cout << "NS: " << NSFoo() << ", Static: " << StaticFoo()
<< ", Inline: " << InlineFoo() << ", Cls: " << Foo::ClsFoo()
<< ", Weak: " << WeakSymbFoo() << std::endl;
return 0;
}();
int main() {}⏎
NS: 0x1018c60e0, Static: 0x1018c60e4, Inline: 0x1018c60d0, Cls: 0x1018c60d4, Weak: 0x1018c60d8
NS: 0x1018c60f0, Static: 0x1018c60f4, Inline: 0x1018c60d0, Cls: 0x1018c60d4, Weak: 0x1018c60d8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment