本文介绍了模板模板参数和。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题(可能是我的)与模板模板参数和铛。下面的玩具示例在g ++ 4.7.0下编译和运行,而不是clang ++ 3.0(基于LLVM 3.0),都是ubuntu 12.04。

I have had problems (possibly mine) with template template parameters and 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.

玩具示例

#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 有一个 inject-class-name ,这是一个独特的语法结构,既是一个 class-name

As noted, it's a Clang bug. AClass there is an injected-class-name, a unique grammatical construct which is both a class-name and a template-name.

另一个解决方法是说 AClass :: template AClass 。这避免需要用其包围的命名空间限定 AClass

Another workaround is to say AClass::template AClass. This avoids needing to qualify AClass with its enclosing namespace.

这篇关于模板模板参数和。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 16:23