C++-如何获取结构的最后一个成员类型以及从现有对象访问它的方法
C++ - How to get structure last member type and a way to access it from existing object?
有没有任何方法可以获得类的最后一个成员类型,以及可能从现有对象(指向类成员的指针)访问它的方法。我需要这一点来实现新的和删除的内置运算符。
你知道我该怎么查吗?
我想做的基本上:
struct S
{
int b;
int arr[];
} ;
struct S1
{
double d;
} ;
last_member_of<S1>::type //type of 'S1::d' - 'double'
last_member_of<S>::type //type of 'S::arr' - 'int []'
last_member_of<S>::value // 'S::*' pointing to 'S::arr'
last_member_of<S1>::value // 'S1::*' pointing to 'S1::d'
这个想法是,如果最后一个成员是灵活的数组(我知道ISO C++不正式支持它,但谁在乎大多数编译器何时真正支持它),我重新定义的运算符将能够为它分配/取消分配额外的存储,自动调用所有构造函数和析构函数
以下是技术方面的操作,假设数组的POD项目类型:
struct S
{
int b;
int arr[1];
auto operator new( size_t const size, int const n )
-> void*
{
return ::operator new( size + (n-1)*sizeof( int ) );
}
void operator delete( void* p )
{
::operator delete( p );
}
};
auto main() -> int
{
auto pS = new( 42 ) S;
// USe it.
delete pS;
}
免责声明:代码不被编译器的手触摸。
对于一般设施,只需对此进行概括即可。例如,您可以将灵活数组的项类型作为模板参数传递。没有必要几乎所有的事情都自动化:显式是好的,隐式是坏的。
说了这么多,对于实用,只需在其中使用std::vector
即可。
附录:OP要求一个直接的解决方案,我有时间编写一个。好吧,除了我没有解决非数组部分的构造函数参数的问题,或者在general中没有解决数组项的构造函数参数或const
访问器的问题。提示OP关于论点:std::forward
是你的朋友。
同样,所有这些购买wrt.使用std::vector
是
- 一种特定的已知存储器布局,例如适合某些现有的C函数
- 单个动态分配
#include <iostream>
#include <functional> // std::function
#include <memory> // std::unique_ptr, std::default_delete
#include <new> // std::operator new( size_t, void* )
#include <stddef.h> // size_t
#include <stdexcept> // std::exception, std::runtime_error
#include <stdlib.h> // EXIT_FAILURE, EXIT_SUCCESS
#include <string> // std::string
namespace cppx {
using std::function;
using std::string;
using std::unique_ptr;
using std::runtime_error;
auto fail( string const& s ) -> bool { throw runtime_error( s ); }
class Non_copyable
{
private:
using This_class = Non_copyable;
This_class& operator=( This_class const& ) = delete;
Non_copyable( This_class const& ) = delete;
public:
Non_copyable() {}
Non_copyable( This_class&& ) {}
};
template< class Common_data, class Item >
class Flexible_array
: public Non_copyable
{
template< class T > friend class std::default_delete;
private:
union Start_of_array
{
Item first_item;
char dummy;
~Start_of_array() {}
Start_of_array(): dummy() {}
};
int size_;
Common_data data_;
Start_of_array items_;
// Private destructor prevents non-dynamic allocation.
~Flexible_array()
{
for( int i = size_ - 1; i >= 0; --i )
{
p_item( i )->~Item();
}
}
Flexible_array( int const size ): size_( size ) {}
// Private allocation function prevents client code dynamic allocation.
// It also servers the purpose of allocating the right size for the array.
static auto operator new( size_t const n_bytes, int const n )
-> void*
{ return ::operator new( n_bytes + (n - 1)*sizeof( Item ) ); }
// Matching operator delete for the case where constructor throws.
static void operator delete( void* const p, int )
{ ::operator delete( p ); }
// General operator delete.
static void operator delete( void* const p )
{ ::operator delete( p ); }
public:
auto size() const -> int { return size_; }
auto data() -> Common_data& { return data_; }
auto p_item( int const i ) -> Item* { return &items_.first_item + i; }
auto item( int const i ) -> Item& { return *p_item( i ); }
void destroy() { delete this; }
static auto create( int const size, function< void( int id, void* p_storage ) > construct )
-> Flexible_array*
{
unique_ptr< Flexible_array > p_flex{ new( size ) Flexible_array( size ) };
for( int i = 0; i < size; ++i )
{
try
{
construct( i, p_flex->p_item( i ) );
}
catch( ... )
{
p_flex->size_ = i;
throw;
}
}
return p_flex.release();
}
static auto create( int const size, Item const& default_value )
-> Flexible_array*
{ return create( size, [&]( int, void* p ) { ::new( p ) Item( default_value ); } ); }
static auto create( int const size )
-> Flexible_array*
{ return create( size, [&]( int, void* p ) { ::new( p ) Item(); } ); }
};
} // namespace cppx
struct X
{
int id;
~X() { std::clog << "X " << id << " destroyedn"; }
X( int const i )
: id( i )
{
if( i == 5 ) { cppx::fail( "Intentional failure of X 5 construction" ); }
std::clog << "X " << id << " createdn";
}
X( X const& other )
: id( other.id )
{
std::clog << "X " << id << " copy-createdn";
}
};
auto main() -> int
{
using namespace std;
try
{
using Flex = cppx::Flexible_array< int, X >;
unique_ptr<Flex> const p{ Flex::create(
7 ,
X( 42 ) // or e.g. "[]( int i, void* p ) { ::new( p ) X( i ); }"
) };
return EXIT_SUCCESS;
}
catch( exception const& x )
{
cerr << "!" << x.what() << "n";
}
return EXIT_FAILURE;
}
不可能自动获取最后一个结构体成员。如果你意识到结构的末尾可能有填充,这很容易理解:根本无法知道末尾是填充还是值:
#include <cstdint>
#include <cstddef>
struct A1 {
uint32_t a;
uint8_t x1;
}; // 3 bytes padding at the end
struct A2 {
uint32_t a;
uint8_t x1;
uint8_t x2;
}; // 2 bytes padding at the end
struct A3 {
uint32_t a;
uint8_t x1;
uint8_t x2;
uint8_t x3;
}; // 1 byte padding at the end
struct A4 {
uint32_t a;
uint8_t x1;
uint8_t x2;
uint8_t x3;
uint8_t x4;
}; // no padding
int main()
{
static_assert(sizeof(A1) == 8);
static_assert(sizeof(A2) == 8);
static_assert(sizeof(A3) == 8);
static_assert(sizeof(A4) == 8);
static_assert(offsetof(A1, x1) == 4);
static_assert(offsetof(A2, x2) == 5);
static_assert(offsetof(A3, x3) == 6);
static_assert(offsetof(A4, x4) == 7);
}
相关文章:
- 通过方法访问结构
- 模板方法访问正向声明的类仅在没有此指针的情况下无法编译
- 类变量无法从类方法访问
- 为什么从静态方法访问非静态方法是糟糕的设计
- 子类是否也在 c++ 中继承私有数据成员?但通过超类的公共方法访问
- 如何从回调方法访问自定义类成员
- 使初始化变量可供多个属性初始化方法访问
- 从c++中的方法访问指向对象的专用指针数组
- C++如何通过回调方法访问成员变量
- "undefined reference"从静态方法访问的模板类的静态成员
- 如何从方法访问类成员
- 类静态方法访问其静态数据成员
- 从静态方法访问非静态成员的工作示例
- 类方法访问其数据成员
- 通过公共方法访问私有成员变量
- 为什么无法从模板方法访问此类自己的受保护成员?
- 通过非静态方法访问静态成员
- JNI保留对对象的全局引用,并使用其他JNI方法访问它.在多个JNI调用中保持C++对象的活动状态
- 如何让一个方法访问其他模板类实例的私有成员
- 方法访问C++中的对象实例化