Пример
Вот упрощенный пример, где я пытаюсь копировать данные в std :: vector , но он не обобщается хорошо: p> p> p> p> p> p> pre -vector , но он не обобщается:
pre wetry: p> p> pre -vector , но он не обобщается. class = "lang-cpp prettyprint-override">
Код: Выделить всё
class DataContainer {
public:
std::vector indices;
};
< /code>
switch(indexType) {
case 1: // uint8_t
const uint8_t* data_u8;
data_u8 = reinterpret_cast(data);
std::memcpy(indices.data(), data_u8, count * sizeof(uint8_t));
break;
case 2: // uint16_t
const uint16_t* data_u16;
data_u16 = reinterpret_cast(data);
std::memcpy(indices.data(), data_u16, count * sizeof(uint16_t));
break;
case 4: // uint32_t
const uint32_t* data_u32;
data_u32 = reinterpret_cast(data);
std::memcpy(indices.data(), data_u32, count * sizeof(uint32_t));
break;
default:
printf("Unsupported index type %d\n", indexType);
return false;
}
[*] Использование std :: variant для хранения различных типов
- обеспечивает безопасность типа, но требует STD :: std () Это также не позволяет мне просто делать indicse.size () net indicse.data () , по крайней мере, не то, что я знаю?и uint16_t ) до uint32_t для простоты.
Код: Выделить всё
#include #include #include using IndexBuffer = std::variant; class DataContainer { public: IndexBuffer indices; }; < /code> хранение всего как std :: vector < /code> < /strong> < /p> uint8_t
- ненужное потребление памяти
< /ul>
< /li>
< /ol>
class DataContainer {
public:
std::vector indices_u8;
std::vector indices_u16;
std::vector indices_u32;
};
< /code>
Как лучше всего справиться с этим? Есть лучшие альтернативы?
Подробнее здесь: https://stackoverflow.com/questions/795 ... ternatives