-
Notifications
You must be signed in to change notification settings - Fork 0
/
sizeof_elipses.cpp
35 lines (30 loc) · 1.13 KB
/
sizeof_elipses.cpp
1
2
3
4
5
6
7
8
9
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
#include <iostream>
template<typename... Args>
static constexpr auto getNumTypes() noexcept
{
return sizeof...(Args);
}
template<typename Arg, typename... Args>
struct sizeof_all
{
static constexpr auto value = sizeof(Arg) + sizeof_all<Args...>::value;
};
// Why can't we just specialize for zero Arguments?
// That's because, the compiler uses the original template for syntax error detection and diagnostic messages
// It is quite possible, but it is the compiler which is not allowing it.
// Therefore, we instead specialize for a single argument
template<typename Arg>
struct sizeof_all<Arg>
{
static constexpr auto value = sizeof(Arg);
};
// NOTE: we can't implement sizeof_all<...> uses function templates; that's because C++ standard doesn't support partial specialization
// for function templates; but it does supports partial specialization for class/struct types.
int main()
{
auto numTypes = getNumTypes<int, float, double>();
std::cout << "sizeof...(int, float, double): " << numTypes << std::endl;
auto size = sizeof_all<int, float, double>::value;
std::cout << "total size of (int, float, double): " << size << std::endl;
return 0;
}