免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2262 | 回复: 2
打印 上一主题 下一主题

[C++] 一个类型转换元编程问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-22 13:05 |只看该作者 |倒序浏览
本帖最后由 幻の上帝 于 2010-12-22 16:30 编辑

求一个元编程实现的根据源类型在编译期选择策略的类型转换模板(C++03),具体需求如下:
1.对于非类类型引用/指针,使用static_cast;
2.对于非多态类的类类型引用/指针,使用static_cast;
3.对于安全的upcast(源类型是目标类型的派生类),使用static_cast;
4.对于非安全的upcast(源类型不是目标类型的派生类)或者非多态虚基类的downcast,产生编译错误;
5.对于源类型是目标类型(去除引用/指针)的虚基类的引用/指针的downcast,使用dynamic_cast;
6.对于其它的crosscast,使用dynamic_cast;
7.对于其它的downcast,使用类似boost::polymorphic_downcast的实现。
貌似过于复杂了……所以我实现不出来- -...
一个问题是不知道是不是有办法能在编译期确定是不是有虚基类……
附现有实现:

  1.     namespace _impl
  2.     {
  3.         template<typename _type, typename _type2, bool _bIsPolymorphicClass>
  4.         struct _general_cast_helper
  5.         {
  6.             inline static _type
  7.             cast(_type2 s)
  8.             {
  9.                 return dynamic_cast<_type>(s);
  10.             }
  11.         };
  12.         template<typename _type, typename _type2>
  13.         struct _general_cast_helper<_type, _type2, false>
  14.         {
  15.             inline static _type
  16.             cast(_type2 s)
  17.             {
  18.                 return static_cast<_type>(s);
  19.             }
  20.         };
  21.         template<typename _type>
  22.         struct _general_cast_helper<_type, _type, true>
  23.         {
  24.             inline static _type
  25.             cast(_type s)
  26.             {
  27.                 return s;
  28.             }
  29.         };
  30.         template<typename _type>
  31.         struct _general_cast_helper<_type, _type, false>
  32.         {
  33.             inline static _type
  34.             cast(_type s)
  35.             {
  36.                 return s;
  37.             }
  38.         };
  39.     }

  40.     /*!
  41.     \brief 类型转换:源类型为多态类时同 dynamic_cast ,否则同 static_cast 。
  42.     */
  43.     template<typename _tDst, typename _tSrc>
  44.     inline _tDst
  45.     general_cast(_tSrc* s)
  46.     {
  47.         return _impl::_general_cast_helper<_tDst, _tSrc*,
  48.             std::tr1::is_polymorphic<_tSrc>::value>::cast(s);
  49.     }
  50.     /*!
  51.     \brief 类型转换:源类型为多态类时同 dynamic_cast ,否则同 static_cast 。
  52.     */
  53.     template<typename _tDst, typename _tSrc>
  54.     inline const _tDst
  55.     general_cast(const _tSrc* s)
  56.     {
  57.         return _impl::_general_cast_helper<_tDst, _tSrc*,
  58.             std::tr1::is_polymorphic<_tSrc>::value>::cast(s);
  59.     }
  60.     /*!
  61.     \brief 类型转换:源类型为多态类时同 dynamic_cast ,否则同 static_cast 。
  62.     */
  63.     template<typename _tDst, typename _tSrc>
  64.     inline _tDst
  65.     general_cast(_tSrc& s)
  66.     {
  67.         return _impl::_general_cast_helper<_tDst, _tSrc&,
  68.             std::tr1::is_polymorphic<_tSrc>::value>::cast(s);
  69.     }
  70.     /*!
  71.     \brief 类型转换:源类型为多态类时同 dynamic_cast ,否则同 static_cast 。
  72.     */
  73.     template<typename _tDst, typename _tSrc>
  74.     inline const _tDst
  75.     general_cast(const _tSrc& s)
  76.     {
  77.         return _impl::_general_cast_helper<_tDst, _tSrc&,
  78.             std::tr1::is_polymorphic<_tSrc>::value>::cast(s);
  79.     }
复制代码

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
2 [报告]
发表于 2010-12-22 13:22 |只看该作者
orz ……

设X是一个class type。
struct A : X {};
struct B : X {};
struct C : A, B {};

如果X含有虚基类的话, C的大小应该比A+B要小?

  1. template<class X>
  2. struct has_virtual_base
  3. {
  4.       struct A : X {};
  5.       struct B : X {};
  6.       struct C : A, B {};
  7.       enum { value = sizeof(C)<sizeof(A)+sizeof(B) };
  8. };
复制代码
可行否?

论坛徽章:
0
3 [报告]
发表于 2010-12-22 16:31 |只看该作者
orz ……

设X是一个class type。
struct A : X {};
struct B : X {};
struct C : A, B {};

如果X含 ...
OwnWaterloo 发表于 2010-12-22 13:22

谢谢。
呃...应该可以用,除非X的虚基类都是没有数据成员的非多态类,估计会被优化掉……不过非多态虚基类downcast用static_cast和dynamic_cast都应付不了,会出来编译错误就行了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP