#include <iostream>
#include <type_traits>
template < typename TypeHead, typename ... TypeTail >
struct TypesList
{
typedef TypeHead head;
typedef TypesList< TypeTail...> tail;
enum { Length = 1 + tail:: Length } ;
static const bool isLast = false ;
template < typename QueryType>
struct Contains
{
static const bool value = std:: is_same < QueryType, TypeHead> :: value ||
tail:: template Contains< QueryType> :: value ;
} ;
} ;
template < typename TypeHead>
struct TypesList< TypeHead>
{
typedef TypeHead head;
enum { Length = 1 } ;
static const bool isLast = true ;
template < typename QueryType>
struct Contains
{
static const bool value = std:: is_same < QueryType, TypeHead> :: value ;
} ;
} ;
template < typename T>
void foo( const T & arg) {
typedef TypesList< int , float , char > AllowedTypes;
static_assert( AllowedTypes:: Contains < T> :: value , "Not allowed type: " ) ;
// do some stuff here
}
struct Yoba { } ;
int main( )
{
auto num = 5 ;
foo( num) ;
Yoba yoba;
foo( yoba) ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dHlwZV90cmFpdHM+Cgp0ZW1wbGF0ZTx0eXBlbmFtZSBUeXBlSGVhZCwgdHlwZW5hbWUuLi4gVHlwZVRhaWw+CnN0cnVjdCBUeXBlc0xpc3QKewoJdHlwZWRlZiBUeXBlSGVhZCBoZWFkOwoJdHlwZWRlZiBUeXBlc0xpc3Q8VHlwZVRhaWwuLi4+IHRhaWw7CgoJZW51bSB7IExlbmd0aCA9IDEgKyB0YWlsOjpMZW5ndGggfTsKCXN0YXRpYyBjb25zdCBib29sIGlzTGFzdCA9IGZhbHNlOwoKCXRlbXBsYXRlPHR5cGVuYW1lIFF1ZXJ5VHlwZT4KCXN0cnVjdCBDb250YWlucwoJewoJCXN0YXRpYyBjb25zdCBib29sIHZhbHVlID0gc3RkOjppc19zYW1lPFF1ZXJ5VHlwZSwgVHlwZUhlYWQ+Ojp2YWx1ZSB8fCAKCQkJdGFpbDo6dGVtcGxhdGUgQ29udGFpbnM8UXVlcnlUeXBlPjo6dmFsdWU7Cgl9Owp9OwoKdGVtcGxhdGU8dHlwZW5hbWUgVHlwZUhlYWQ+CnN0cnVjdCBUeXBlc0xpc3Q8VHlwZUhlYWQ+CnsKCXR5cGVkZWYgVHlwZUhlYWQgaGVhZDsKCgllbnVtIHsgTGVuZ3RoID0gMSB9OwoJc3RhdGljIGNvbnN0IGJvb2wgaXNMYXN0ID0gdHJ1ZTsKCgl0ZW1wbGF0ZTx0eXBlbmFtZSBRdWVyeVR5cGU+CglzdHJ1Y3QgQ29udGFpbnMKCXsKCQlzdGF0aWMgY29uc3QgYm9vbCB2YWx1ZSA9IHN0ZDo6aXNfc2FtZTxRdWVyeVR5cGUsIFR5cGVIZWFkPjo6dmFsdWU7Cgl9Owp9OwoKdGVtcGxhdGU8dHlwZW5hbWUgVD4Kdm9pZCBmb28oY29uc3QgVCAmYXJnKSB7Cgl0eXBlZGVmIFR5cGVzTGlzdDxpbnQsIGZsb2F0LCBjaGFyPiBBbGxvd2VkVHlwZXM7CgoJc3RhdGljX2Fzc2VydChBbGxvd2VkVHlwZXM6OkNvbnRhaW5zPFQ+Ojp2YWx1ZSwgIk5vdCBhbGxvd2VkIHR5cGU6ICIpOwoKCS8vIGRvIHNvbWUgc3R1ZmYgaGVyZQp9CgpzdHJ1Y3QgWW9iYSB7fTsKCmludCBtYWluKCkKewoJYXV0byBudW0gPSA1OwoJZm9vKG51bSk7CgoJWW9iYSB5b2JhOwoJZm9vKHlvYmEpOwoKCXJldHVybiAwOwp9Cg==
compilation info
prog.cpp: In instantiation of ‘void foo(const T&) [with T = Yoba]’:
prog.cpp:53:10: required from here
prog.cpp:40:2: error: static assertion failed: Not allowed type:
static_assert(AllowedTypes::Contains<T>::value, "Not allowed type: ");
^
stdout