5 c++ g++ undefined-behavior vptr c++11
我正在尝试实现类似于此处描述的系统.也就是说,(ab)使用vtable修改来改变运行时的对象行为.这是我尝试在我正在开发的C++项目中创建高效类型通用包装器的一部分.
例如,如果您无法访问它,请使用memcpy()和this指针复制vtable :
void setType( const DataType& newType )
{
memcpy( this, &newType, sizeof(DataType) );
}
Run Code Online (Sandbox Code Playgroud)
但是,我对此方法存在一个问题:我没有目标类的对象来复制vtable,并且不希望创建一个,因为某些类型的构造成本很高.
有没有办法访问vtable,它将被放置到给定类的对象中而没有该类的对象?
如果它在某种程度上是可移植的,那将是更好的,但我基本上已经辞职,因为它是特定于编译器的; 因此,如果没有其他选项,只接受GCC/G ++方法.我们还假设我只关心在相当标准的操作系统和体系结构上构建它.
我正在使用C++ 11,应该以某种方式帮助它.
编辑:我想完全清楚,我知道这种行为是多么危险.我对这个想法更感兴趣,也许它在非常有控制的情况下应用它的狭隘应用,而不是我对生产软件的好主意,尽管我的介绍可能会提出建议.
struct many_vtable {
void(*dtor)(void*);
void(*print)(void const*,std::ostream&);
};
template<class T>struct tag_t{};
template<class T>constexpr tag_t<T> tag = {};
template<class T>
many_vtable const* make_many_vtable(tag_t<T>){
static const many_vtable retval = {
// dtor
[](void* p){
reinterpret_cast<T*>(p)->~T();
},
// print
[](void const*p, std::ostream& os){
os<<*reinterpret_cast<T const*>(p);
}
};
return &retval;
}
struct many {
many_vtable const* vtable=nullptr;
std::aligned_storage_t<100, alignof(double)> buff;
void clear(){if(vtable) vtable->dtor(&buff);vtable=nullptr;}
~many(){ clear(); }
many()=default;
many(many const&)=delete; // not yet supported
many& operator=(many const&)=delete; // not yet supported
explicit operator bool()const{return vtable!=nullptr;}
template<class T,class...Args>
void emplace(Args&&...args){
static_assert(alignof(T) <= alignof(double), "not enough alignment");
static_assert(sizeof(T) <= 100, "not enough size");
clear();
::new((void*)&buff) T(std::forward<Args>(args)...);
vtable=make_many_vtable(tag<T>);
}
friend std::ostream& operator<<(std::ostream& os, many const&m){
if(!m.vtable) return os;
m.vtable->print(&m.buff, os);
return os;
}
};
Run Code Online (Sandbox Code Playgroud)
这是手动vtable设计。它可以存储最多 100 个字节的任何内容,其对齐方式小于可以打印到流的双精度值。
“擦除”更多(或不同)的操作很容易。例如,能够复制/移动到另一个许多。
它不违反标准,并且与链接示例具有类似的开销。
many m;
m.emplace<int>(3);
std::cout << m << '\n';
m.emplace<double>(3.14);
std::cout << m << '\n';
Run Code Online (Sandbox Code Playgroud)
这是手动 vtable,因为我们基本上是手动重新实现 vtable 概念。