施瓦茨计数器和可变模板
Schwarz Counter and variadic template
对于从未听说过"Schwarz Counter"的人,请参阅此维基百科页面。
我试图用c++ 11模板实现这个习惯用法,一切都很好,除了模板参数不是默认可构造的时候。这是我的实现:(见问题的代码注释)
#include <type_traits>
template<typename T>
class Single {
public:
static T& get() {
return reinterpret_cast<T&>(instance.storage);
}
private:
struct Instance {
enum { kSize = sizeof(T), kAlign = std::alignment_of<T>::value };
int counter;
typename std::aligned_storage<kSize, kAlign>::type storage;
template<typename... Arg>
Instance(Arg&&... param) {
if (counter++ == 0) {
new (&storage) T(param...);
}
}
~Instance() {
if (--counter == 0) {
reinterpret_cast<T&>(storage).~T();
}
}
};
static Instance instance;
};
// The following won't compile if T isn't default constructible.
// I want to get rid of the definition in this case, and let the
// users of this template to provide their own definitions.
// I'd like to keep the it when T is default constructible, because
// most of the time T is (according to my experience, most global
// objects are constructed without arguments, singletons are
// essentially global objects).
//
// So, basically I want something like this:
// #if std::is_default_constructible<T>::value
// template<typename T>
// ...;
// #endif
// Could this be done in C++11 ? If not, how about C++14 ?
template<typename T>
typename Single<T>::Instance Single<T>::instance;
顺便说一句,如果类是默认可构造的,用这个模板定义一个单例是非常简单的,例如:
class Y {
friend class Single<Y>;
Y(int, int) { std::cout << "Y()n"; }
~Y() { std::cout << "~Y()n"; }
};
class X {
friend class Single<X>;
X() { std::cout << "X()n"; }
~X() { std::cout << "~X()n"; }
};
int main() {
auto& x = Single<X>::get();
// auto& g = Single<Y>::get(); // oops
return 0;
}
创建两个类。一个用于默认可构造T,另一个用于非。
调度你的主实现只使用默认的可构造实例,如果它是默认的可构造实例。
template<class T,class=void>
struct WhichSingle {
using type=Single<T>;
};
template<class T>
struct WhichSingle<T,typename std::enable_if<
!std::is_default_constructible<T>::value
>::type> {
using type=SingleNoDefault<T>;
};
template<class X>
using Bob = typename WhichSignle<X>::type;
且Bob<X>
为Single
或SingleNoDefault
。
更好的名字和减少重复的工作是可以做到的,但这只是繁重的工作。
相关文章:
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- 循环在计数器中不起作用
- python集合的C++等价物是什么.计数器
- Python 集合.计数器,如何避免重复查找
- 请解释字谜的代码,我看不懂计数器数组,每个值已经是0
- 为什么瓦尔格林德在不释放恶意内存后没有报告任何问题?
- std::shared_ptr 自定义参考计数器
- 英特尔 TBB 程序不会终止,可能会误用参考计数器
- 瓦尔格林德的内存泄漏使用新的
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- 使用莱布尼茨公式的 Pi 近似
- 代码厨师问题伊什瓦拉(应许之地)
- 不正确的比较和交换计数器输出用于快速排序功能
- OpenSSL API,使用GCM(伽罗瓦计数器模式)进行AES加密
- C++ 在循环中添加计数器变量并再次初始化其值
- C++程序什么都不做,但瓦尔格林德显示内存分配
- 带有thread_local的 c++ 施瓦茨计数器
- 我同时使用了莱布尼茨和瓦利斯公式来估算π但输出数字错误,我不知道我哪里出错了
- 施瓦茨计数器和可变模板
- 漂亮/施瓦茨计数器,标准兼容