Boost库中的ref库 怎样用不可拷贝的对象传值?

korewayume 2015-08-13 04:54:46
在学习罗剑锋著的《Boost程序库完全开发指南》的ref库的时候遇到几个问题。希望大家可以帮我解惑。
学习ref这一章的时候看到前面的引文介绍ref的时候看到这样一句话
引用
首先我们将学习一个小的工具类ref,它是本章其他库的基础,可以包装对象的引用,在传递参数时消除对象拷贝的代价,或者将不可拷贝的对象变为可以拷贝

然后看到后面又有类似的话:

但是书中的例子用ref包装的都是可以拷贝的对象,于是我自己写了一个noncopyable的类,再用ref传值,还是不能拷贝

#include <boost/ref.hpp>
#include <boost/noncopyable.hpp>
using namespace boost;

class A:public noncopyable //A是一个不可拷贝的类
{
public:
A(){};
~A(){};
};

void print(A a){}; //需要传值的函数

void main()
{
A a; //不可拷贝的对象
print(ref(a)); //用ref封包装依旧报错
}
引用
error C2248: “boost::noncopyable_::noncopyable::noncopyable”: 无法访问 private 成员(在“boost::noncopyable_::noncopyable”类中声明)


请问是我的用法不对,还是对ref的理解有错误?

谁能帮我举个将不可拷贝的对象变为可以拷贝的例子吗?

谢谢大家
...全文
165 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
korewayume 2015-08-13
  • 打赏
  • 举报
回复
引用 7楼珍惜生命远离CPP 的回复:
[quote=引用 6 楼 korewayume 的回复:] [quote=引用 5 楼 akirya 的回复:] ref reference_wrapper就是为了解决bind的函数有引用参数的问题。 要是没ref reference_wrapper的话,bind 有引用参数的函数就逻辑错误了。 reference_wrapper的目的,作者都没搞清楚。
意思就是说ref就是为bind而存在的,脱离了bind,那这个ref就没有太大意义??[/quote] 是啊,这个顶多算辅助类。[/quote]谢谢了,我当时一看到可以把不可拷贝的变为可以拷贝的,还以为ref有多牛逼
sdghchj 2015-08-13
  • 打赏
  • 举报
回复
"将不可拷贝的对象变为可以拷贝"是错的吧。 ref函数只是将参数转成了std::reference_wrapper,而std::reference_wrapper也仅仅只是将该参数对象的指针进行了封装,可隐式转换其隐用而已。 所以print(ref(a)); 与A& b=a; print(b);是等价的,print(b)还是不拷贝b。所以ref不是这样用的。
  • 打赏
  • 举报
回复
引用 6 楼 korewayume 的回复:
[quote=引用 5 楼 akirya 的回复:] ref reference_wrapper就是为了解决bind的函数有引用参数的问题。 要是没ref reference_wrapper的话,bind 有引用参数的函数就逻辑错误了。 reference_wrapper的目的,作者都没搞清楚。
意思就是说ref就是为bind而存在的,脱离了bind,那这个ref就没有太大意义??[/quote] 是啊,这个顶多算辅助类。
korewayume 2015-08-13
  • 打赏
  • 举报
回复
引用 5 楼 akirya 的回复:
ref reference_wrapper就是为了解决bind的函数有引用参数的问题。 要是没ref reference_wrapper的话,bind 有引用参数的函数就逻辑错误了。 reference_wrapper的目的,作者都没搞清楚。
意思就是说ref就是为bind而存在的,脱离了bind,那这个ref就没有太大意义??
  • 打赏
  • 举报
回复
引用 4 楼 korewayume 的回复:
[quote=引用 2 楼 akirya 的回复:] 一般是这样使用的 http://en.cppreference.com/w/cpp/utility/functional/ref
用ref(a).get()也不行
#include <functional>
#include <iostream>
 
void f(int& n1, int& n2, const int& n3)
{
    std::cout << "In function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    ++n1; // increments the copy of n1 stored in the function object
    ++n2; // increments the main()'s n2
    // ++n3; // compile error
}
 
int main()
{
    int n1 = 1, n2 = 2, n3 = 3;
    std::function<void()> bound_f = std::bind(f, n1, std::ref(n2), std::cref(n3));
    n1 = 10;
    n2 = 11;
    n3 = 12;
    std::cout << "Before function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    bound_f();
    std::cout << "After function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
}
这里的void f(int& n1, int& n2, const int& n3)都是传的引用啦,所以不管参数是否是可拷贝的[/quote] ref reference_wrapper就是为了解决bind的函数有引用参数的问题。 要是没ref reference_wrapper的话,bind 有引用参数的函数就逻辑错误了。 reference_wrapper的目的,作者都没搞清楚。
korewayume 2015-08-13
  • 打赏
  • 举报
回复
引用 2 楼 akirya 的回复:
一般是这样使用的 http://en.cppreference.com/w/cpp/utility/functional/ref
用ref(a).get()也不行
#include <functional>
#include <iostream>
 
void f(int& n1, int& n2, const int& n3)
{
    std::cout << "In function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    ++n1; // increments the copy of n1 stored in the function object
    ++n2; // increments the main()'s n2
    // ++n3; // compile error
}
 
int main()
{
    int n1 = 1, n2 = 2, n3 = 3;
    std::function<void()> bound_f = std::bind(f, n1, std::ref(n2), std::cref(n3));
    n1 = 10;
    n2 = 11;
    n3 = 12;
    std::cout << "Before function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    bound_f();
    std::cout << "After function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
}
这里的void f(int& n1, int& n2, const int& n3)都是传的引用啦,所以不管参数是否是可拷贝的
  • 打赏
  • 举报
回复
korewayume 2015-08-13
  • 打赏
  • 举报
回复
引用 1 楼 akirya 的回复:
你的print用法不对
那应该怎么用我这个函数没有些功能,只是为了测试能不能值传递 我当然知道用引用传递参数是可以通过的
  • 打赏
  • 举报
回复
你的print用法不对

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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