[英]template-id does not match any template delcaration
I'm getting a frustrating compiler error I can't seem to work around. 我遇到了一个令人沮丧的编译器错误,似乎无法解决。 It's to do with the template specialization but I can't see what's wrong...
这与模板专业化有关,但我看不出有什么问题...
../../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
The code is as follows: 代码如下:
#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
A full class template specialization is not a template any more, it is a regular class. 完整的类模板专业化不再是模板,而是常规类。 Hence you don't need template<> when defining its members:
因此,在定义其成员时,不需要template <>:
lock_guard<null_mutex>::lock_guard(null_mutex&)
{
/* do nothing */
}
lock_guard<null_mutex>::~lock_guard()
{
/* do nothing */
}
也许这不是错误的原因,但是您不需要头文件中的专业化代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.