简体   繁体   English

模板模板参数和clang

[英]template template parameters and clang

I have had problems (possibly mine) with template template parameters and clang.我在模板模板参数和 clang 方面遇到了问题(可能是我的)。 The following toy example compiles and runs under g++ 4.7.0, not clang++ 3.0 (based on LLVM 3.0), both ubuntu 12.04.以下玩具示例在 g++ 4.7.0 下编译并运行,而不是在 clang++ 3.0(基于 LLVM 3.0)下,两者都是 ubuntu 12.04。

Toy example (test_1.cpp):玩具示例(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;                                                                                        
}                                                                                           

clang output:叮当输出:

*****@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.        

As noted, it's a Clang bug.如前所述,这是一个 Clang 错误。 AClass there is an injected-class-name , a unique grammatical construct which is both a class-name and a template-name . AClass有一个注入类名,这是一个独特的语法结构,它既是类名又是模板名

Another workaround is to say AClass::template AClass .另一种解决方法是说AClass::template AClass This avoids needing to qualify AClass with its enclosing namespace.这避免了需要用其封闭的命名空间来限定AClass

The same thing happens for me with Clang 3.3.在 Clang 3.3 中我也发生了同样的事情。

The solution -- or workaround -- from this SO question is to replace AClass with ::AClass on lines 47 and 50, and then it compiles happily.解决方案——或解决方法——来自这个 SO question是在第 47 和 50 行用::AClass替换AClass ,然后它愉快地编译。

To be honest template template parameters make my head hurt.说实话模板模板参数让我头疼。 The referenced question suggests it's a Clang bug, but I'm not enough of an expert to be able to say.引用的问题表明这是一个 Clang 错误,但我不是专家,无法说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM