feat(templates): add AddressOf function and the corresponding testing

This commit is contained in:
_Redstone_c_ 2022-02-09 17:07:47 +08:00
parent 63fdd34789
commit 2bc852b63c
3 changed files with 49 additions and 0 deletions

View File

@ -9,6 +9,7 @@ NAMESPACE_MODULE_BEGIN(Utility)
void TestTemplates()
{
TestInvoke();
TestMiscellaneous();
}
NAMESPACE_UNNAMED_BEGIN
@ -44,6 +45,41 @@ void TestInvoke()
always_check(TempE == 123);
}
NAMESPACE_UNNAMED_BEGIN
template <typename T>
struct TTestStructA
{
T* Pad;
T* Data;
TTestStructA(T* InData) : Pad(nullptr), Data(InData) { }
~TTestStructA() { delete Data; }
T** operator&() { return &Data; }
};
template <typename T>
int32 TestFunctionB(TTestStructA<T>* Ptr)
{
return 0;
}
template <typename T>
int32 TestFunctionB(T** Ptr)
{
return 1;
}
NAMESPACE_UNNAMED_END
void TestMiscellaneous()
{
TTestStructA<int32> ObjectA(new int32(3));
always_check(TestFunctionB(&ObjectA) == 1);
always_check(TestFunctionB(AddressOf(ObjectA)) == 0);
always_check(AddressOf(TestMiscellaneous) == &TestMiscellaneous);
}
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END

View File

@ -82,6 +82,18 @@ constexpr T Exchange(T& A, U&& B)
template <typename T>
T&& DeclVal();
template <typename T> requires TIsObject<T>::Value
constexpr T* AddressOf(T& Object)
{
return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const volatile char&>(Object)));
}
template <typename T> requires !TIsObject<T>::Value
constexpr T* AddressOf(T& Object)
{
return &Object;
}
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END

View File

@ -8,6 +8,7 @@ NAMESPACE_MODULE_BEGIN(Utility)
void REDCRAFTUTILITY_API TestTemplates();
void REDCRAFTUTILITY_API TestInvoke();
void REDCRAFTUTILITY_API TestMiscellaneous();
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)