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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完整的类模板特化不再是模板,而是常规类。因此你不需要 template<>定义其成员时:
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:
也许这不是错误的原因,但您不需要头文件中的专门化代码。
Perhaps it is not the cause of the error, but you don't need the specialization code in the header file.