This file contains hidden or 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 <iostream> | |
#include <string> | |
#include <map> | |
class Mapleton { | |
public: | |
// Returns a reference to static instance of this class | |
// based on provided string identifier | |
static Mapleton& instance(const std::string& id) { | |
static std::map<std::string, Mapleton> instances; |
This file contains hidden or 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 <iostream> | |
#include <string> | |
class Singleton { | |
public: | |
// Returns a reference to static instance of this class | |
static Singleton& instance() { | |
static Singleton instance; | |
return instance; | |
} |
This file contains hidden or 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
Class inheritance | BaseClass visibility | Inheriting class visibility | |
---|---|---|---|
public | public | public | |
protected | protected | ||
private | none | ||
protected | public | protected | |
protected | protected | ||
private | none | ||
private | public | private |
This file contains hidden or 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 <vector> | |
std::size_t counter = 0; | |
std::mutex cout_mutex; | |
void foo() | |
{ | |
{ | |
std::lock_guard<std::mutex> lock(cout_mutex); | |
std::cout << "T" << get_thread_id() << " increasing counter" << std::endl; | |
} |
This file contains hidden or 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
T0 increasing counter | |
T1 increasing counter | |
================== | |
WARNING: ThreadSanitizer: data race (pid=4888) | |
Write of size 8 at 0x0000011b4980 by thread T2: | |
#0 foo() ??:? (a.out+0x4c521b) | |
#1 void std::_Bind_simple<void (*())()>::_M_invoke<>(std::_Index_tuple<>) ??:? (a.out+0x4cbeb5) | |
#2 std::_Bind_simple<void (*())()>::operator()() ??:? (a.out+0x4cbe48) | |
#3 std::thread::_Impl<std::_Bind_simple<void (*())()> >::_M_run() ??:? (a.out+0x4cbaac) | |
#4 std::this_thread::__sleep_for(std::chrono::duration<long, std::ratio<1l, 1l> >, std::chrono::duration<long, std::ratio<1l, 1000000000l> >) ??:? (libstdc++.so.6+0xb8c7f) |
This file contains hidden or 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 <iostream> | |
class BaseClass { | |
public: | |
void say_hello() { | |
std::cout << "Hello"; | |
} | |
protected: | |
void say_world() { | |
std::cout << "world!"; |
This file contains hidden or 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
// test_class.hpp | |
#include <iosfwd> | |
class TestClass { | |
public: | |
TestClass() = default; | |
void hello_to_stream(std::stringstream& ss) const noexcept; | |
}; | |
This file contains hidden or 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 <iostream> | |
#include <bitset> | |
#include <type_traits> | |
namespace test { | |
// Allows ~ operator for any enum class in the same namespace | |
template<class Enum, class = typename std::enable_if<std::is_enum<Enum>::value, Enum>::type> | |
inline Enum operator~(Enum a) { | |
return static_cast<Enum>(~static_cast<typename std::underlying_type<Enum>::type>(a)); | |
} |
This file contains hidden or 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 <thread> | |
#include <atomic> | |
std::size_t get_thread_id() noexcept { | |
static std::atomic<std::size_t> thread_idx{0}; | |
thread_local std::size_t id = thread_idx; | |
thread_idx++; | |
return id; | |
} |