Reputation: 4487
I would like to achieve something like that:
template<class... Args>
class MyClass{
public:
MyClass(){
for(auto arg : {sizeof(Args)...})
std::cout<<arg<<std::endl;
}
};
But with one simple exception. The type char* should return 0(or everything else, what will be distinct from an int).
Upvotes: 1
Views: 218
Reputation: 111120
How about the following?
/* heavily borrowed from IBM's variadic template page */
#include <iostream>
using namespace std;
/*
template<typename T> struct type_size{
operator int(){return sizeof( T );}
};
template<> struct type_size <char *>{
operator int(){return 0;}
};
*/
/* as per Mattieu M.'s suggestion */
template<typename T> constexpr size_t type_size(T dummy) {
return sizeof dummy;
}
constexpr size_t type_size(char *){
return 0;
}
template <typename...I> struct container{
container(){
int array[sizeof...(I)]={type_size<I>()...};
printf("container<");
for(int count = 0; count<sizeof...(I); count++){
if(count>0){
printf(",");
}
printf("%d", array[count]);
}
printf(">\n");
}
};
int main(void){
container<int, short, char *> g;
}
Upvotes: 1
Reputation: 35449
template<typename T>
std::size_t size()
{
return sizeof(T);
}
template<>
std::size_t size<char*>()
{
return 0;
}
template<class... Args>
class MyClass{
public:
MyClass()
{
std::initializer_list<char> { (std::cout << size<Args>(), void(), char {})... };
}
};
Although in truth I have an EXPAND
macro that hides the std::initializer_list
ugliness (not to mention void()
+ comma operator trick) such that it would look like EXPAND( std::cout << size<Args>() )
.
Upvotes: 0