tdutils: as<T> is safer now
GitOrigin-RevId: 8bdb4ce3e84c8a753d76502e830bcfcd3c0bd28b
This commit is contained in:
parent
cba0f62ba9
commit
bc991da146
@ -113,11 +113,11 @@ class As {
|
||||
As(void *ptr) : ptr_(ptr) {
|
||||
}
|
||||
As(As &&) = default;
|
||||
const As<T> &operator=(const As &new_value) const {
|
||||
As<T> &operator=(const As &new_value) && {
|
||||
memcpy(ptr_, new_value.ptr_, sizeof(T));
|
||||
return *this;
|
||||
}
|
||||
const As<T> &operator=(const T new_value) const {
|
||||
As<T> &operator=(const T new_value) && {
|
||||
memcpy(ptr_, &new_value, sizeof(T));
|
||||
return *this;
|
||||
}
|
||||
|
@ -593,6 +593,8 @@ TEST(Misc, As) {
|
||||
ASSERT_EQ(123, as<int>((const char *)buf));
|
||||
ASSERT_EQ(123, as<int>((char *)buf));
|
||||
char buf2[100];
|
||||
//auto x = as<int>(buf2);
|
||||
//x = 44342; //CE
|
||||
as<int>(buf2) = as<int>(buf);
|
||||
ASSERT_EQ(123, as<int>((const char *)buf2));
|
||||
ASSERT_EQ(123, as<int>((char *)buf2));
|
||||
|
Reference in New Issue
Block a user