- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
#include <clcpp/clcpp.h>
#include <clcpp/FunctionCall.h>
// Reflect the entire namespace and implement each class
clcpp_reflect(TestClassImpl)
namespace TestClassImpl
{
class A
{
public:
A()
{
x = 1;
y = 2;
z = 3;
}
int x, y, z;
};
struct B
{
B()
{
a = 1.5f;
b = 2.5f;
c = 3.5f;
}
float a, b, c;
};
}
clcpp_impl_class(TestClassImpl::A)
clcpp_impl_class(TestClassImpl::B)
void TestConstructorDestructor(clcpp::Database& db)
{
const clcpp::Class* ca = clcpp::GetType<TestClassImpl::A>()->AsClass();
const clcpp::Class* cb = clcpp::GetType<TestClassImpl::B>()->AsClass();
TestClassImpl::A* a = (TestClassImpl::A*)new char[sizeof(TestClassImpl::A)];
TestClassImpl::B* b = (TestClassImpl::B*)new char[sizeof(TestClassImpl::B)];
CallFunction(ca->constructor, a);
CallFunction(cb->constructor, b);
CallFunction(ca->destructor, a);
CallFunction(cb->destructor, b);
delete [] (char*) a;
delete [] (char*) b;
}
https://github.com/Celtoys/clReflect/blob/master/src/clReflectTest/TestClassImpl.cpp
Steve_Brown 14.06.2022 13:47 # 0
guest6 14.06.2022 14:03 # 0
а тут не надо выравнивать рази?
Гост, помогай
Soul_re@ver 14.06.2022 14:14 # 0
https://eel.is/c++draft/expr.new#16
When a new-expression calls an allocation function and that allocation has not been extended, the new-expression passes the amount of space requested to the allocation function as the first argument of type std::size_t.
That argument shall be no less than the size of the object being created; it may be greater than the size of the object being created only if the object is an array and the allocation function is not a non-allocating form ([new.delete.placement]).
For arrays of char, unsigned char, and std::byte, the difference between the result of the new-expression and the address returned by the allocation function shall be an integral multiple of the strictest fundamental alignment requirement of any object type whose size is no greater than the size of the array being created.
[Note 9: Because allocation functions are assumed to return pointers to storage that is appropriately aligned for objects of any type with fundamental alignment, this constraint on array allocation overhead permits the common idiom of allocating character arrays into which objects of other types will later be placed.
— end note]
guest6 14.06.2022 14:22 # 0
спасибо
Support 15.06.2022 22:33 # 0
guest6 14.06.2022 14:35 # 0
Soul_re@ver 14.06.2022 14:51 # 0
Если есть вероятность появления таких типов, то да, на случай непопадания в нужное выравнивание, нужно аллоцировать памяти больше на alignof(TestClassImpl::A) - alignof(std::max_align_t), и затем применить std::align.
guest6 14.06.2022 15:39 # 0
хм, я с плейсмент нью создавать объекты в куче вручную, но там надо было специально выравнивание заказывать, а не тупо malloc делать, хотя инструмент для выравнивания появился пожжы, чем плейсмент ню (хз как раньше люди жили)
Support 14.06.2022 14:51 # 0
Steve_Brown 16.06.2022 14:27 # 0
guest6 16.06.2022 14:51 # 0
пальцев засохшего клопа, которого, видно, носил в сапоге уже несколько дней.