其他分享
首页 > 其他分享> > c – 模板模板参数和clang

c – 模板模板参数和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.

标签:c,templates,clang,clang-2
来源: https://codeday.me/bug/20191006/1861974.html