模板 ID 不匹配任何模板声明 [英] template-id does not match any template delcaration

查看:63
本文介绍了模板 ID 不匹配任何模板声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

代码如下:

 #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:

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

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

这篇关于模板 ID 不匹配任何模板声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆