简体   繁体   中英

C++ template explicit instantation, with template argument being a class template

I have a linker issue when trying to explicitly instantiate a class. Using C++11, LLVM 5.1 . Here is a minimal working example:

declaration.h :

template <class T>
class Box {
    public :
    template <class _T>
    using Box_sub = Box<_T>;
};

//argument 'int N' is only used to remove ambiguity about class instantiation
template < template <class T> class Tbox, int N >
class A {
    public :
    A();
};

A_implementation.h :

template < template <class T> class Tbox, int N >
A<Tbox,N>::A() {}

explicit_instantiation.cpp :

#include "declaration.h"
#include "A_implementation.h"

template class A<Box,1>;
template class A<Box<int>::Box_sub,2>;

main.cpp :

#include "declaration.h"

int main() {

    A<Box,1> a1;
    A<Box<int>::Box_sub,2> a2;
    return 0;
}

And here is the linker error :

Undefined symbols for architecture x86_64:
"A<Box<int>::Box_sub, 2>::A()", referenced from:
  _main in main.o

Looks like compiler consider the second explicit instantiation as something different than main's second declaration. I don't see why. Probably a problem as with class template being embedded in another one.

Actually the problem is probably closely linked to another, as I previously asked about dynamic casting : C++ dynamic downcasting to class template having template template parameter being a class template or an alias template

A trick was proposed, but in the case of explicit instantiation I was wondering if another (simpler) trick was possible.


Solution given in comments : it is a bug in Clang 3.4

评论中给出的解决方案:这是Clang 3.4中的一个错误

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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