免费注册 查看新帖 |

Chinaunix

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

关于 boost::any, 谁能帮我解答这个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-11-28 21:22 |只看该作者 |倒序浏览
#include <vector>;
#include <string>;
#include <boost/any.hpp>;
using namespace std;
using namespace boost;

class my
{
public:
        void test(){}
};

vector<any>; any_vector;

template<typename T>; T& get(int index)
{
        return any_cast<T>;(any_vector[index]);
}

int main()
{
        any_vector.push_back(10);
        any_vector.push_back(string("hello");
        any_vector.push_back(my());

        any_vector[0]=string("nana";

        get<my>;(2).test();
        return 0;

}

/// gcc3.4.2 编译出错
error: invalid initialization of non-const reference of type 'my&' from a temporary of type 'my'

//什么地方产生了临时变量??

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2004-11-28 22:27 |只看该作者

关于 boost::any, 谁能帮我解答这个问题

归根结底,算了,不归根结底了,一步步来看一下.


首先:any_cast<T>;(any_vector[index]); 它调用的是:
  1.     template<typename ValueType>;
  2.     ValueType any_cast(const any & operand)
  3.     {
  4.         const ValueType * result = any_cast<ValueType>;(&operand);
  5.         if(!result)
  6.             boost::throw_exception(bad_any_cast());
  7.         return *result;
  8.     }
复制代码
而此函数转而调用的是:
  1.     template<typename ValueType>;
  2.     const ValueType * any_cast(const any * operand)
  3.     {
  4.         return any_cast<ValueType>;(const_cast<any *>;(operand));
  5.     }
复制代码
而此函数又转而调用:
  1.     template<typename ValueType>;
  2.     ValueType * any_cast(any * operand)
  3.     {
  4.         return operand && operand->;type() == typeid(ValueType)
  5.                     ? &static_cast<any::holder<ValueType>; *>;(operand->;content)->;held
  6.                     : 0;
  7.     }
复制代码
看到了吗?返回值是从static_cast转换而来的临时对象的地址.你应该知道static_cast会构造临时对象吧?

还有一个问题就是,你的get是返回const类型而调用的成员函数不是const的.所以把test声明成const.或者你直接返回值也行.而不是引用:如下:
  1. #include <vector>;
  2. #include <string>;
  3. #include <boost/any.hpp>;
  4. using namespace std;
  5. using namespace boost;

  6. class my
  7. {
  8. public:
  9. void test()const {}
  10. };

  11. vector<any>; any_vector;

  12. template<typename T>; const T get(int index)
  13. {
  14. return any_cast<T>;(any_vector[index]);
  15. }

  16. int main()
  17. {
  18. any_vector.push_back(10);
  19. any_vector.push_back(string("hello"));
  20. any_vector.push_back(my());

  21. any_vector[0]=string("nana");

  22. get<my>;(2).test();
  23. return 0;

  24. }
复制代码

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
3 [报告]
发表于 2004-11-28 22:51 |只看该作者

关于 boost::any, 谁能帮我解答这个问题

上面的解释应该不对.明天再来改,今天没时间了.sorry先.

我没有gcc3.4版本,用的是gcc3.3.1,编译信息很不一样.

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
4 [报告]
发表于 2004-11-28 22:57 |只看该作者

关于 boost::any, 谁能帮我解答这个问题

  1. #include <vector>;
  2. #include <string>;
  3. #include <boost/any.hpp>;
  4. using namespace std;
  5. using namespace boost;

  6. class my
  7. {
  8. public:
  9. void test()const{}
  10. };

  11. vector<any>; any_vector;

  12. template<typename T>;
  13. const T& get(int index)
  14. {
  15. return any_cast<T>;(any_vector[index]);
  16. }

  17. int main()
  18. {
  19. any_vector.push_back(10);
  20. any_vector.push_back(string("hello"));
  21. any_vector.push_back(my());

  22. any_vector[0]=string("nana");

  23. get<my>;(2).test();
  24. return 0;

  25. }
复制代码
看编译信息是什么?

论坛徽章:
0
5 [报告]
发表于 2004-11-29 10:50 |只看该作者

关于 boost::any, 谁能帮我解答这个问题

const T& get(int index)
这个是可以通过的

但我需要的不是const返回引用

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
6 [报告]
发表于 2004-11-29 11:13 |只看该作者

关于 boost::any, 谁能帮我解答这个问题

原帖由 "zlqian" 发表:
const T& get(int index)
这个是可以通过的

但我需要的不是const返回引用
不返回const引用,不能引用到临时对象啊.或者你用const_cast?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP