65,186
社区成员




template<>
struct __type_traits<int>{
typedef __true_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
typedef __true_type is_POD_type;
};
#include <iostream>
#include <vector>
using namespace std;
struct __true_type
{
};
struct __false_type
{
};
template<class T>
struct __type_traits
{
typedef __false_type has_trivial_default_constructor;
typedef __false_type has_trivial_copy_constructor;
typedef __false_type has_trivial_assignment_operator;
typedef __false_type has_trivial_destructor;
typedef __false_type is_POD_type;
};
template<>
struct __type_traits<int>
{
typedef __true_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
typedef __true_type is_POD_type;
};
class A;
template<>
struct __type_traits<A>
{
typedef __true_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __false_type has_trivial_destructor;
typedef __true_type is_POD_type;
};
class A
{
public:
A():p(new int(3))
{
}
~A()
{
cout<<"You know"<<endl;
delete p;
}
private:
int *p;
};
template <class T>
void destroy(T &obj)
{
_destroy(obj,typename __type_traits<T>::has_trivial_destructor());
}
template <class T>
void _destroy(T &obj,__true_type)
{
//do nothing
}
template <class T>
void _destroy(T &obj,__false_type)
{
obj.~T();
}
int main()
{
int *p=(int*)malloc(sizeof(int));
*p=2;
destroy(*p);
free(p);
A *q=(A*)malloc(sizeof(A));
q=new (q) A();
destroy(*q);
free(q);
return 0;
}
.
#include <iostream>
using namespace std;
struct __true_type
{
};
struct __false_type
{
};
template<class T>
struct __type_traits
{
typedef __false_type has_trivial_default_constructor;
typedef __false_type has_trivial_copy_constructor;
typedef __false_type has_trivial_assignment_operator;
typedef __false_type has_trivial_destructor;
typedef __false_type is_POD_type;
};
template<>
struct __type_traits<int>
{
typedef __true_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
typedef __true_type is_POD_type;
};
template<>
struct __type_traits<double>
{
typedef __true_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
typedef __true_type is_POD_type;
};
template<>
struct __type_traits<char>
{
typedef __true_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
typedef __true_type is_POD_type;
};
//...
template <class ForwardIterator,class T>
inline void destroy(ForwardIterator first,ForwardIterator last)
{
typedef typename __type_traits<T>:://是不是编译器的原因,我这里::之后会出现成员的
}
int main()
{
system("pause");
return 0;
}