c++ - 模板模板参数和 clang

标签 c++ templates clang 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;                                                                                        
}                                                                                           

clang 输出:

*****@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 有一个injected-class-name,一个独特的语法结构,既是class-name又是template-name .

另一种解决方法是说 AClass::template AClass。这避免了需要用其封闭的命名空间来限定 AClass

关于c++ - 模板模板参数和 clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31086955/

相关文章:

c++ - 如何从可变参数模板参数创建 std::tuple<>?

python.clang AST解析: getting c++ template argument of field declaration

c - 禁用 clang-tidy 诊断

c++ - C++中使用一个成员函数计算二叉树的高度

c++ - [ '(' token 之前的预期标识符或 '{' 错误]

c++ - 如何检查 std::filesystem::copy 是否结束?

c - 检测宏参数是否为类型名

c++ - 如何使用 boost lambda 用新对象填充指针 vector

javascript - meteor 0.8.0 : While building the application: Unexpected closing template tag

c++ - 在模板化成员函数的返回类型中使用 std::enable_if 时的编译器差异