我得到一個令人沮喪的編譯器錯誤,我似乎無法解決。這是與模板專門做的,但我看不出有什麼不對......template-id不匹配任何模板排版
../../include/thread/lock_guard.inl:23: error: template-id 'lock_guard<>' for 'thread::lock_guard<thread::null_mutex>::lock_guard(thread::null_mutex&)' does not match any template declaration
../../include/thread/lock_guard.inl:23: error: invalid function declaration
../../include/thread/lock_guard.inl:29: error: template-id 'lock_guard<>' for 'thread::lock_guard<thread::null_mutex>::~lock_guard()' does not match any template declaration
../../include/thread/lock_guard.inl:29: error: invalid function declaration
的代碼如下:
#include "thread/mutex.hpp"
namespace thread {
template <typename T>
class lock_guard
{
public:
lock_guard(T& lock);
~lock_guard();
private:
mutable T& m_lock;
mutable int m_state;
};
template <>
class lock_guard<null_mutex>
{
public:
lock_guard(null_mutex&);
~lock_guard();
};
} //namespace
#include "thread/lock_guard.inl"
------------------------------------
#include "thread/lock_guard.hpp"
namespace thread {
template <typename T>
lock_guard<T>::lock_guard(T& lock)
: m_lock(lock),
m_state(lock.lock())
{
/* do nothing */
}
template <typename T>
lock_guard<T>::~lock_guard()
{
if(0 == m_state)
{
m_lock.unlock();
}
}
template <>
lock_guard<null_mutex>::lock_guard(null_mutex&)
{
/* do nothing */
}
template <>
lock_guard<null_mutex>::~lock_guard()
{
/* do nothing */
}
} //namespace
什麼是`可變的T&m_lock;`應該這樣做?它不應該編譯。 – 2011-01-14 18:48:23