[英]boost unable to move a scoped_lock with gcc
The following compiles under VS2010 (Express) but not gcc (4.6.2 here). 以下编译在VS2010(Express)下,但不是gcc(此处为4.6.2)。
Lockable.h: Lockable.h:
#include <boost/thread/mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
template<typename T>
class LockedProxy : boost::noncopyable
{
public:
inline LockedProxy(boost::mutex & m, T * obj)
:lock(m),
t(obj)
{}
inline LockedProxy(const LockedProxy && other)
:lock(std::move(other.lock)),
t(std::move(other.t))
{}
inline T * operator->() { return t; }
inline const T * operator->() const { return t; }
inline const T & operator*() const { return *t; }
inline T & operator*() { return *t; }
private:
boost::interprocess::scoped_lock<boost::mutex> lock;
T * t;
};
template<typename T>
class Lockable
{
public:
// Convenience typefed for subclasses to use
typedef T LockableObjectType;
inline Lockable(const T & t)
:lockableObject(t)
{}
inline LockedProxy<LockableObjectType> GetLockedProxy() {
return LockedProxy<LockableObjectType>(mutex, &lockableObject);
}
protected:
LockableObjectType lockableObject;
boost::mutex mutex;
};
main.cpp: main.cpp中:
#include <iostream>
#include <string.h>
#include "Lockable.h"
void f(Lockable<std::string> & str)
{
auto proxy = str.GetLockedProxy();
*proxy = "aa";
proxy->append("bb");
std::cout << "str = " << *proxy << std::endl;
}
void g(Lockable<int> & i)
{
{ // reduce lock's lifespan
auto proxy = i.GetLockedProxy();
*proxy = 321;
}
// relock, lock lives for the statement
std::cout << "i = " << *i.GetLockedProxy() << std::endl;
}
int main()
{
Lockable<std::string> str("abc");
//Can't use str here, it is not locked
f(str);
Lockable<int> i(123);
g(i);
return 0;
}
The errors: 错误:
In file included from main.cpp:3:0:
C:/Users/DrGibbs/Documents/code/boost_1_46_0/boost/interprocess/sync/scoped_lock.hpp: In constructor 'LockedProxy<T>::LockedProxy(const LockedProxy<T>&&) [with T = std::basic_string<char>, LockedProxy<T> = LockedProxy<std::basic_string<char> >]':main.cpp:7:37: instantiated from here
C:/Users/DrGibbs/Documents/code/boost_1_46_0/boost/interprocess/sync/scoped_lock.hpp:56:4: error: 'boost::interprocess::scoped_lock<Mutex>::scoped_lock(const boost::interprocess::scoped_lock<Mutex>&)[with Mutex = boost::mutex, boost::interprocess::scoped_lock<Mutex> = boost::interprocess::scoped_lock<boost::mutex>]' is privateLockable.h:14:29: error: within this context
C:/Users/DrGibbs/Documents/code/boost_1_46_0/boost/interprocess/sync/scoped_lock.hpp: In constructor 'LockedProxy<T>::LockedProxy(const LockedProxy<T>&&) [with T = int, LockedProxy<T> = LockedProxy<int>]':
main.cpp:18:36: instantiated from here
C:/Users/DrGibbs/Documents/code/boost_1_46_0/boost/interprocess/sync/scoped_lock.hpp:56:4: error: 'boost::interprocess::scoped_lock<Mutex>::scoped_lock(const boost::interprocess::scoped_lock<Mutex>&)[with Mutex = boost::mutex, boost::interprocess::scoped_lock<Mutex> = boost::interprocess::scoped_lock<boost::mutex>]' is private
Lockable.h:14:29: error: within this context
Well as far as I understand, in LockedProxy
's move-constructor, the scoped_lock
is not moved but copy-constructed, which really should not work. 据我所知,在
LockedProxy
的移动构造函数中, scoped_lock
不会移动而是复制构造,这实际上不应该起作用。 Shouldn't the std::move
guanrantee its move-construction ? std::move
不应该保证它的移动构造吗? What am I missing ? 我错过了什么?
Your move constructor declares its parameter const
: 你的移动构造函数声明它的参数
const
:
inline LockedProxy(const LockedProxy && other)
It should be declared non-const: 它应该声明为非const:
inline LockedProxy(LockedProxy && other)
Your std::move(other.lock)
is taking other.lock
as a const reference, and so returning a const rvalue reference const boost::interprocess::scoped_lock<boost::mutex> &&
, which cannot be passed to the move constructor of boost::interprocess::scoped_lock<boost::mutex>
. 你的
std::move(other.lock)
将other.lock
作为const引用,因此返回一个const rvalue引用const boost::interprocess::scoped_lock<boost::mutex> &&
,这些不能传递给move boost::interprocess::scoped_lock<boost::mutex>
构造函数。
See also C++0x const RValue reference as function parameter , which explains that const rvalue references are almost entirely useless. 另请参阅C ++ 0x const RValue引用作为函数参数 ,它解释了const rvalue引用几乎完全无用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.