Pimplイディオムをローカルストレージイディオムに適用したい:
mytype.h
class mytype {
struct Impl;
enum{ storage = 20; }
char m_storage[ storage ];
Impl* PImpl() { return (Impl*)m_storage; }
public:
mytype();
~mytype();
void myMethod();
};
mytype.cpp
#include "mytype.h"
struct mytype::Impl {
int foo;
void doMethod() { foo = (foo-1)*3; };
}
mytype::mytype() {
new (PImpl()) Impl(); // placement new
//check this at compile-time
static_assert( sizeof(Impl) == mytype::storage );
//assert alignment?
}
mytype::~mytype() {
PImpl()->~();
}
void mytype::myMethod() {
PImpl()->doMethod();
}
このアプローチで私が抱えている唯一の懸念は、の調整ですm_storage
。char
intと同じように整列されるとは限りません。アトミックには、さらに制限の厳しい配置要件があります。アライメント値を定義(およびアサート)する機能を提供するストレージを宣言するためのchar配列よりも優れたものを探しています。何か知っていますか?多分ブーストライブラリはすでにこれを行っていますか?