mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-10-28 19:04:45 +08:00
- const README: rewrite the space-saving claim per issue #5 follow-ups (constant-expression const vs #define immediates; which const qualify as constant expressions) - const README: fix the class-section diagnosis per issue #338 (overload resolution picks the const overload; the restriction only surfaces once that overload is removed) - sizeof README: virtual-inheritance result is compiler-dependent (GCC merges vbptr+vfptr -> 16, MSVC keeps them separate -> 32) #246 - static README: new section on internal linkage / access restriction with anonymous-namespace comparison #142 - 10-day practice: full problem list table + fixed day links #136 - new design_pattern/singleton/singleton_default.hpp: reusable thread-safe CRTP singleton template, compile+concurrency tested #303
This commit is contained in:
58
design_pattern/singleton/singleton_default.hpp
Normal file
58
design_pattern/singleton/singleton_default.hpp
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* @file singleton_default.hpp
|
||||
* @brief 可复用的线程安全单例模板基类(CRTP)
|
||||
* 受 issue #303 启发:https://github.com/Light-City/CPlusPlusThings/issues/303
|
||||
* 参考 boost::container::singleton_default 的思路。
|
||||
*
|
||||
* 用法:
|
||||
* class MyConfig : public SingletonDefault<MyConfig> {
|
||||
* friend class SingletonDefault<MyConfig>;
|
||||
* private:
|
||||
* MyConfig() = default; // 构造私有,交由基类创建
|
||||
* public:
|
||||
* int value = 42;
|
||||
* };
|
||||
* MyConfig::instance().value = 1;
|
||||
*
|
||||
* 编译:g++ -std=c++11 main.cpp
|
||||
* @author 光城
|
||||
* @version v1
|
||||
* @date 2026-10
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
|
||||
template <class T>
|
||||
class SingletonDefault {
|
||||
public:
|
||||
SingletonDefault(const SingletonDefault&) = delete;
|
||||
SingletonDefault& operator=(const SingletonDefault&) = delete;
|
||||
|
||||
static T& instance() {
|
||||
// C++11 起,局部静态变量的初始化由标准保证线程安全("magic statics")
|
||||
static T inst;
|
||||
return inst;
|
||||
}
|
||||
|
||||
protected:
|
||||
SingletonDefault() = default;
|
||||
~SingletonDefault() = default;
|
||||
};
|
||||
|
||||
/**
|
||||
* 若需要 C++03 或对 magic statics 不放心的环境,可用 call_once 版本:
|
||||
*
|
||||
* template <class T>
|
||||
* class SingletonDefaultOnce {
|
||||
* public:
|
||||
* static T& instance() {
|
||||
* std::call_once(once_, [] { inst_.reset(new T); });
|
||||
* return *inst_;
|
||||
* }
|
||||
* private:
|
||||
* static std::once_flag once_;
|
||||
* static std::unique_ptr<T> inst_;
|
||||
* };
|
||||
*/
|
||||
Reference in New Issue
Block a user