簡體   English   中英

模板模板參數和clang

[英]template template parameters and clang

我在模板模板參數和 clang 方面遇到了問題(可能是我的)。 以下玩具示例在 g++ 4.7.0 下編譯並運行,而不是在 clang++ 3.0(基於 LLVM 3.0)下,兩者都是 ubuntu 12.04。

玩具示例(test_1.cpp):

#include <iostream>                                                                                 
#include <memory>                                                                                   

struct AFn                                                                                          
{                                                                                                   
   void operator()()                                                                                
     {                                                                                              
    ; // do something                                                                               
     }                                                                                              
};                                                                                                  

template<typename T>                                                                                
  struct impl                                                                                       
{                                                                                                   
   T *backpointer_;                                                                                 
};                                                                                                  

template<typename S, template <typename> class T>                                                   
  struct implT                                                                                      
{                                                                                                   
   T<S> *backpointer_;                                                                              
};                                                                                                  

template<typename>                                                                                  
  class AClass;                                                                                     

template<>                                                                                          
  struct implT<AFn, AClass>                                                                         
{                                                                                                   
   implT(std::string message) :                                                                     
     message_(message)                                                                              
       {}                                                                                           

   void operator()()                                                                                
     {                                                                                              
    std::cout << " : " << message_ << std::endl;                                                    
     }                                                                                              

   std::string message_;                                                                            
};                                                                                                  


template<typename Fn>                                                                               
class AClass                                                                                        
{                                                                                                   
 private:                                                                                           
   std::shared_ptr<implT<Fn, AClass> > p_;                                                          
 public:                                                                                            
   AClass(std::string message) :                                                                    
     p_(std::make_shared<implT<Fn, AClass> >(message))                                              
       {}                                                                                           
   void call_me()             
     {                                                                                              
    p_->operator()();                                                                               
     }                                                                                              
};                                                                                                  


int main(int argc, char **argv)                                                                     
{                                                                                                   
   AClass<AFn> *A = new AClass<AFn>("AClass<AFn>");                                                 
   A->call_me();                                                                                    

   delete A;                                                                                        

   return 0;                                                                                        
}                                                                                           

叮當輸出:

*****@ely:~$ clang++ -std=c++11 test_1.cpp -o test_1
test_1.cpp:47:30: error: template argument for template template parameter must be a class template or
      type alias template
   std::shared_ptr<implT<Fn, AClass> > p_;
                         ^
test_1.cpp:47:40: error: C++ requires a type specifier for all declarations
   std::shared_ptr<implT<Fn, AClass> > p_;
                                   ^~
test_1.cpp:50:36: error: template argument for template template parameter must be a class template or
  type alias template
 p_(std::make_shared<implT<Fn, AClass> >(message))
                               ^
3 errors generated.
                                                                                        I can't make sense of the first error. It compiles and runs fine with gcc/g++ 4.7.0. Any help would be appreciated.        

如前所述,這是一個 Clang 錯誤。 AClass有一個注入類名,這是一個獨特的語法結構,它既是類名又是模板名

另一種解決方法是說AClass::template AClass 這避免了需要用其封閉的命名空間來限定AClass

在 Clang 3.3 中我也發生了同樣的事情。

解決方案——或解決方法——來自這個 SO question是在第 47 和 50 行用::AClass替換AClass ,然后它愉快地編譯。

說實話模板模板參數讓我頭疼。 引用的問題表明這是一個 Clang 錯誤,但我不是專家,無法說。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM