Template template and clang options

I had problems (maybe mine) with template template and clang options. The following toy example compiles and runs under g ++ 4.7.0, rather than clang ++ 3.0 (based on LLVM 3.0), like ubuntu 12.04.

Toy example (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. 
+7
c ++ templates clang clang ++
source share
2 answers

As already noted, this is a Clang bug. AClass has a name with the introduced class, a unique grammatical construction, which is both the name of the class and the name of the template.

Another workaround is AClass::template AClass . This avoids qualifying AClass with its encompassing namespace.

+7
source share

The same thing happens to me with Clang 3.3.

The solution - or a workaround - from this SO question is to replace AClass with ::AClass with lines 47 and 50, and then it compiles with joy.

To be honest, the template template options make my head hurt. This question suggests that this is Klang’s mistake, but the expert is not enough for me to be able to say.

+2
source share

All Articles