简体   繁体   English

template-id与任何模板替代都不匹配

[英]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.

相关问题 显式特化 - 模板 ID 不匹配任何模板声明 - Explicit specialization - template-id does not match any template declaration C++ 模板 ID 与任何模板都不匹配 - C++ template-id does not match any template C ++ template-id与任何模板都不匹配? - C++ template-id does not match any template? 模板 ID 不匹配任何模板声明 - template-id does not match any template declaration 模板类专门化:template-id与任何模板声明都不匹配 - template class specialization : template-id does not match any template declaration 显式实例化可变参数构造函数:template-id与任何模板声明都不匹配 - Explicitly instantiating variadic constructor: template-id does not match any template declaration template-id与任何模板声明GNU gcc编译器都不匹配 - template-id does not match any template declaration GNU gcc compiler 模板化函数..错误:template-id与任何模板声明都不匹配 - Templated Functions.. ERROR: template-id does not match any template declaration cpp文件中的模板专业化实现导致template-id不匹配错误 - Template specialization implementation in cpp file causes template-id does not match error 模板ID与任何模板声明都不匹配 - template id does not match any template declaration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM