求助 stl 里的__type_traits

noein115 2011-01-03 07:34:29

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;
};


我照着stl 写了这样的 int long double 等等 很多段

然后写一个函数

template <class ForwardIterator,class T>
inline void destroy(ForwardIterator first,ForwardIterator last){
typedef typename __type_traits<T>::

到这里时 ::后 没出现任何选项

状态栏里显示 " .或 -> 左边的表达式具有无法解析的类型"


怎么会无效?
...全文
94 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2011-01-03
  • 打赏
  • 举报
回复
楼主写错了,你需要的是iterator_traits,你的destroy无法识别T类型是什么,模板函数是自动推导的.

要停电了,凑活看
#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;
}

.



we_sky2008 2011-01-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 noein115 的回复:]
没编译...

destroy函数是在另外一个 construct.h 头文件里写的..
[/Quote]
呵呵,找到原因就好了,
we_sky2008 2011-01-03
  • 打赏
  • 举报
回复

#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;
}



noein115 2011-01-03
  • 打赏
  • 举报
回复
没编译...

destroy函数是在另外一个 construct.h 头文件里写的..

ryfdizuo 2011-01-03
  • 打赏
  • 举报
回复
你编译有错吗?
type trait只是萃取类型。。。

64,439

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧