Added size_of<>

This commit is contained in:
John Wellbelove 2019-05-15 22:31:30 +01:00
parent 8fb468e708
commit e3fa5b912c
2 changed files with 39 additions and 0 deletions

View File

@ -598,6 +598,21 @@ namespace etl
typedef const type_t* const_pointer;
typedef const type_t* const const_pointer_const;
};
//***************************************************************************
// size_of
//***************************************************************************
template <typename T>
struct size_of
{
static const size_t size = sizeof(T);
};
template <>
struct size_of<void>
{
static const size_t size = 1;
};
}
#endif

View File

@ -41,6 +41,20 @@ namespace std
#include "etl/type_traits.h"
#include <type_traits>
namespace
{
struct TestData { };
}
namespace etl
{
template <>
struct etl::size_of<TestData>
{
static const size_t size = 20;
};
}
namespace
{
// A class to test non-fundamental types.
@ -728,4 +742,14 @@ namespace
CHECK_EQUAL(1, v1);
CHECK_EQUAL(2, v2);
}
//*************************************************************************
TEST(size_of)
{
CHECK_EQUAL(1, etl::size_of<void>::size);
CHECK_EQUAL(1, etl::size_of<char>::size);
CHECK_EQUAL(2, etl::size_of<short>::size);
CHECK_EQUAL(4, etl::size_of<int>::size);
CHECK_EQUAL(20, etl::size_of<TestData>::size);
}
}