2011-01-14 35 views
1

我得到一個令人沮喪的編譯器錯誤,我似乎無法解決。這是與模板專門做的,但我看不出有什麼不對......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 
+0

什麼是`可變的T&m_lock;`應該這樣做?它不應該編譯。 – 2011-01-14 18:48:23

回答

14

一個完整的類模板特不是一個模板更多,這是一個普通班。因此,界定其成員時,你不需要模板<>:

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

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

編寫它的正確方法可能是[令人困惑](http://llvm.org/bugs/show_bug.cgi?id=8708)有時:) – 2011-01-14 18:27:15

2

也許它不是錯誤的原因,但你並不需要在頭文件中的代碼專業化。

+0

你實際上並不想它那裏。如果標題包含在多個翻譯單元中,則會出現多個定義錯誤。 – 2011-01-14 18:10:55