2012-09-17 3 views
0

다음은 VS2010 (익스프레스)에서 컴파일되지만 gcc (4.6.2)에서는 컴파일되지 않습니다.부스트 gcc로 scoped_lock을 이동할 수 없습니다

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 :

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

오류 : I 유 등 지금까지 잘

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 

nderstand에서 LockedProxy의 이동 생성자에서 scoped_lock은 이동되지 않고 복사 생성되었으므로 실제로 작동하면 안됩니다. std::move guanrantee는 이동 건설을해서는 안됩니까? 내가 뭘 놓치고 있니?

inline LockedProxy(const LockedProxy && other) 

그것은 선언해야 const가 아닌 :

답변

2

당신의 이동 생성자는 매개 변수 const를 선언 귀하는 const를 기준으로 other.lock을 복용하고, 그래서 CONST를 rvalue를 반환 std::move(other.lock)

inline LockedProxy(LockedProxy && other) 

참조 const boost::interprocess::scoped_lock<boost::mutex> &&boost::interprocess::scoped_lock<boost::mutex>의 이동 생성자에 전달할 수 없습니다.

또한 C++0x const RValue reference as function parameter을 참조하십시오. const rvalue 참조는 거의 쓸모가 없다고 설명합니다.

+0

AAAH! 그건 내 절름발이 야. – Gabriel

관련 문제