Given code like the following:
foo.cpp
#include"foo.h"
template <int X>
void bar::foo(){
cout << X << endl;
}
static void tea(){
bar().foo<1>();
bar().foo<2>();
bar().foo<3>();
}
foo.h
class bar {
public:
template <int X>
void foo();
};
main.cpp
...
bar().foo<1>();
bar().foo<2>();
bar().foo<3>();
...
Is such a pattern allowed in any version of C++?
Edit:
Why is this disallowed when explicit instantiation with template void bar::foo<1>()
(and etc) would allow this to link correctly?
C++ compilers are not required to actually generate symbols for anything unless you force it with an extern declaration. They aggresively inline functions, which might remove all evidence of your instantiation. I tried out your example, and it linked with -O0
, but not with -O3
.
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.