aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2020-04-15 19:33:02 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2020-04-15 19:33:08 +0300
commit4759d2ad96dd4d874b8d901a0b469bace17aa4a4 (patch)
treedecc917ac2092bbba710f00f3d18714cd7a9f69f
parentssh-tunnel: grammar fix (diff)
downloadjekyll-theme-4759d2ad96dd4d874b8d901a0b469bace17aa4a4.tar.gz
jekyll-theme-4759d2ad96dd4d874b8d901a0b469bace17aa4a4.zip
shorter code snippets
-rw-r--r--_posts/2015-07-03-std-call-once-bug.md66
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/shared.hpp3
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/weird/another.cpp6
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/weird/main.cpp6
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/another.cpp3
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/main.cpp3
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/shared.hpp6
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/main.cpp3
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/proxy.cpp3
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/shared.hpp3
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/main.cpp3
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/proxy.cpp3
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/shared.hpp3
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/main.cpp3
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/proxy.cpp3
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/shared.hpp14
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/ok/another.cpp19
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/ok/main.cpp19
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/weird/another.cpp10
-rw-r--r--_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/weird/main.cpp10
20 files changed, 80 insertions, 109 deletions
diff --git a/_posts/2015-07-03-std-call-once-bug.md b/_posts/2015-07-03-std-call-once-bug.md
index 544c616..0aa0249 100644
--- a/_posts/2015-07-03-std-call-once-bug.md
+++ b/_posts/2015-07-03-std-call-once-bug.md
@@ -36,11 +36,9 @@ using C++11 facilities like this:
#include <mutex>
template <typename Derived>
-class Singleton
-{
+class Singleton {
public:
- static Derived& get_instance()
- {
+ static Derived& get_instance() {
std::call_once(initialized_flag, &initialize_instance);
return Derived::get_instance_unsafe();
}
@@ -49,15 +47,13 @@ protected:
Singleton() = default;
~Singleton() = default;
- static Derived& get_instance_unsafe()
- {
+ static Derived& get_instance_unsafe() {
static Derived instance;
return instance;
}
private:
- static void initialize_instance()
- {
+ static void initialize_instance() {
Derived::get_instance_unsafe();
}
@@ -76,8 +72,7 @@ Now other classes can inherit from `Singleton`, implementing the singleton
pattern effortlessly:
```c++
-class Logger : public Singleton<Logger>
-{
+class Logger : public Singleton<Logger> {
private:
Logger() = default;
~Logger() = default;
@@ -93,11 +88,9 @@ trickery, and the implementation would be much simpler, i.e. something like
this:
```c++
-class Logger
-{
+class Logger {
public:
- static Logger& get_instance()
- {
+ static Logger& get_instance() {
static Logger instance;
return instance;
}
@@ -146,18 +139,15 @@ constructor, and everything looked fine at first glance.
#include <chrono>
#include <thread>
-class Logger : public Singleton<Logger>
-{
+class Logger : public Singleton<Logger> {
public:
- Logger& operator<<(const char* msg)
- {
+ Logger& operator<<(const char* msg) {
// Actual logging is stripped for brevity.
return *this;
}
private:
- Logger()
- {
+ Logger() {
// Opening log files, etc.
std::this_thread::sleep_for(std::chrono::seconds{3});
}
@@ -167,11 +157,9 @@ private:
friend class Singleton<Logger>;
};
-class Duke : public Singleton<Duke>
-{
+class Duke : public Singleton<Duke> {
private:
- Duke()
- {
+ Duke() {
Logger::get_instance() << "started Duke's initialization";
// It's a lot of work to be done.
std::this_thread::sleep_for(std::chrono::seconds{10});
@@ -191,25 +179,23 @@ Like in this example:
```c++
#include <thread>
-namespace
-{
- void get_logger()
- {
- entered(__FUNCTION__);
- Logger::get_instance();
- exiting(__FUNCTION__);
- }
+namespace {
+
+void get_logger() {
+ entered(__FUNCTION__);
+ Logger::get_instance();
+ exiting(__FUNCTION__);
+}
+
+void get_duke() {
+ entered(__FUNCTION__);
+ Duke::get_instance();
+ exiting(__FUNCTION__);
+}
- void get_duke()
- {
- entered(__FUNCTION__);
- Duke::get_instance();
- exiting(__FUNCTION__);
- }
}
-int main()
-{
+int main() {
std::thread t1{&get_duke};
std::thread t2{&get_logger};
t1.join();
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/shared.hpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/shared.hpp
index 91313f2..796ea85 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/shared.hpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/shared.hpp
@@ -1,7 +1,6 @@
#pragma once
-inline int shared()
-{
+inline int shared() {
static int n = 0;
return ++n;
}
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/weird/another.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/weird/another.cpp
index 668516a..330ba80 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/weird/another.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/weird/another.cpp
@@ -2,12 +2,10 @@
#include <iostream>
-inline void shared()
-{
+inline void shared() {
std::cout << "another.cpp: shared()\n";
}
-void another()
-{
+void another() {
shared();
}
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/weird/main.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/weird/main.cpp
index 4e32fb5..e278b9f 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/weird/main.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/inline/weird/main.cpp
@@ -2,13 +2,11 @@
#include <iostream>
-inline void shared()
-{
+inline void shared() {
std::cout << "main.cpp: shared()\n";
}
-int main()
-{
+int main() {
shared();
another();
return 0;
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/another.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/another.cpp
index d8d53ae..f13b3a1 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/another.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/another.cpp
@@ -1,7 +1,6 @@
#include "another.hpp"
#include "shared.hpp"
-void another()
-{
+void another() {
Test test;
}
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/main.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/main.cpp
index 8d2043f..b3118c1 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/main.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/main.cpp
@@ -1,8 +1,7 @@
#include "another.hpp"
#include "shared.hpp"
-int main()
-{
+int main() {
Test test;
another();
return 0;
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/shared.hpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/shared.hpp
index ef2d525..ef4da34 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/shared.hpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/separate_method_definitions/shared.hpp
@@ -2,13 +2,11 @@
#include <iostream>
-struct Test
-{
+struct Test {
Test();
};
-inline Test::Test()
-{
+inline Test::Test() {
static int x = 0;
std::cout << ++x << '\n';
}
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/main.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/main.cpp
index 7fbeb56..fde1a43 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/main.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/main.cpp
@@ -3,8 +3,7 @@
#include <iostream>
-int main()
-{
+int main() {
std::cout << shared() << '\n';
std::cout << proxy() << '\n';
return 0;
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/proxy.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/proxy.cpp
index a8eef26..78e4611 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/proxy.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/proxy.cpp
@@ -1,7 +1,6 @@
#include "proxy.hpp"
#include "shared.hpp"
-int proxy()
-{
+int proxy() {
return shared();
}
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/shared.hpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/shared.hpp
index 6f812ab..647f49e 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/shared.hpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static/shared.hpp
@@ -1,7 +1,6 @@
#pragma once
-static int shared()
-{
+static int shared() {
static int n = 0;
return ++n;
}
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/main.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/main.cpp
index 7fbeb56..fde1a43 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/main.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/main.cpp
@@ -3,8 +3,7 @@
#include <iostream>
-int main()
-{
+int main() {
std::cout << shared() << '\n';
std::cout << proxy() << '\n';
return 0;
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/proxy.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/proxy.cpp
index a8eef26..78e4611 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/proxy.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/proxy.cpp
@@ -1,7 +1,6 @@
#include "proxy.hpp"
#include "shared.hpp"
-int proxy()
-{
+int proxy() {
return shared();
}
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/shared.hpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/shared.hpp
index a669247..28de441 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/shared.hpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/static_and_inline/shared.hpp
@@ -1,7 +1,6 @@
#pragma once
-static inline int shared()
-{
+static inline int shared() {
static int x = 0;
return ++x;
}
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/main.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/main.cpp
index 7fbeb56..fde1a43 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/main.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/main.cpp
@@ -3,8 +3,7 @@
#include <iostream>
-int main()
-{
+int main() {
std::cout << shared() << '\n';
std::cout << proxy() << '\n';
return 0;
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/proxy.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/proxy.cpp
index a8eef26..78e4611 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/proxy.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/proxy.cpp
@@ -1,7 +1,6 @@
#include "proxy.hpp"
#include "shared.hpp"
-int proxy()
-{
+int proxy() {
return shared();
}
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/shared.hpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/shared.hpp
index c64cc41..e21a00c 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/shared.hpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespace_and_inline/shared.hpp
@@ -1,10 +1,10 @@
#pragma once
-namespace
-{
- inline int shared()
- {
- static int x = 0;
- return ++x;
- }
+namespace {
+
+inline int shared() {
+ static int x = 0;
+ return ++x;
+}
+
}
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/ok/another.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/ok/another.cpp
index 10128ef..cc7556d 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/ok/another.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/ok/another.cpp
@@ -2,17 +2,18 @@
#include <iostream>
-namespace
-{
- struct Test
- {
- Test() { std::cout << "another.cpp: Test::Test()\n"; }
+namespace {
+
+struct Test {
+ Test() {
+ std::cout << "another.cpp: Test::Test()\n";
+ }
+
+ float y = 1.;
+};
- float y = 1.;
- };
}
-void another()
-{
+void another() {
Test test;
}
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/ok/main.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/ok/main.cpp
index c0c0664..e383ded 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/ok/main.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/ok/main.cpp
@@ -2,18 +2,19 @@
#include <iostream>
-namespace
-{
- struct Test
- {
- Test() { std::cout << "main.cpp: Test::Test()\n"; }
+namespace {
+
+struct Test {
+ Test() {
+ std::cout << "main.cpp: Test::Test()\n";
+ }
+
+ int x = 1;
+};
- int x = 1;
- };
}
-int main()
-{
+int main() {
Test test;
std::cout << test.x << '\n';
another();
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/weird/another.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/weird/another.cpp
index 8a125ec..0e0bff9 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/weird/another.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/weird/another.cpp
@@ -2,14 +2,14 @@
#include <iostream>
-struct Test
-{
- Test() { std::cout << "another.cpp: Test::Test()\n"; }
+struct Test {
+ Test() {
+ std::cout << "another.cpp: Test::Test()\n";
+ }
float y = 1.;
};
-void another()
-{
+void another() {
Test test;
}
diff --git a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/weird/main.cpp b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/weird/main.cpp
index 8ab8fb8..abd42b7 100644
--- a/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/weird/main.cpp
+++ b/_posts/snippets/static_vs_inline_vs_unnamed_namespaces/unnamed_namespaces/weird/main.cpp
@@ -2,15 +2,15 @@
#include <iostream>
-struct Test
-{
- Test() { std::cout << "main.cpp: Test::Test()\n"; }
+struct Test {
+ Test() {
+ std::cout << "main.cpp: Test::Test()\n";
+ }
int x = 1;
};
-int main()
-{
+int main() {
Test test;
std::cout << test.x << '\n';
another();