template-id 与任何模板声明不匹配

发布于 2024-10-12 09:14:28 字数 1832 浏览 2 评论 0原文

我遇到了令人沮丧的编译器错误,我似乎无法解决。这与模板专业化有关,但我看不出有什么问题......

../../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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

却一份温柔 2024-10-19 09:14:28

完整的类模板特化不再是模板,而是常规类。因此你不需要 template<>定义其成员时:

lock_guard<null_mutex>::lock_guard(null_mutex&)
{
    /* do nothing */
}

lock_guard<null_mutex>::~lock_guard()
{
    /* do nothing */
}

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:

lock_guard<null_mutex>::lock_guard(null_mutex&)
{
    /* do nothing */
}

lock_guard<null_mutex>::~lock_guard()
{
    /* do nothing */
}
依 靠 2024-10-19 09:14:28

也许这不是错误的原因,但您不需要头文件中的专门化代码。

Perhaps it is not the cause of the error, but you don't need the specialization code in the header file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文