Saturday, February 4, 2017

Implementing a type trait

I copied/adapted this code from Polukhin's Boost book:

#include <vector>
#include <boost/type_traits/integral_constant.hpp>
#include <cstdio>
template <class T>
struct is_stdvector: boost::false_type {};
template <class T, class Allocator>
struct is_stdvector<std::vector<T, Allocator> >: boost::true_type
{};
int main()
{
printf("int=%s\n", is_stdvector<int>::value ? "true" : "false");
printf("vector<int>=%s\n", is_stdvector<std::vector<int>>::value ? "true" : "false");
printf("vector<char*>=%s\n", is_stdvector<std::vector<char*>>::value ? "true" : "false");
return 0;
}
When run:

kuyu@ub16:~/dkuyu/Dropbox/practice/cpp/polukhin/ch04/implement_type_trait$ g++ -std=c++11 -I/home/kuyu/dkuyu/bin/boost_1_60_0 1.cpp && ./a.out
int=false
vector<int>=true
vector<char*>=true



No comments:

Post a Comment