explicit instantiation vs std::vector template how: gotcha
Just continuation of closed theme
http://cplusplus.com/forum/general/83798/
Now have a kind of library with <algorithms> involved. Crashes again.
main file ex.cpp:
1 2 3 4 5 6 7
|
#include "sub.h"
int main(int argc, const char **argv)
{
CMain<TCbDoubleBridgeBase_Candidates> cs;
//
return 0;
}
|
and library: sub.cpp+sub.h. Want to do instantiation encapsulated:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#ifndef sub_h
#define sub_h
#include <vector>
#include <iostream>
using namespace std;
struct TCbDoubleBridgeBase_Candidates
{
public:
int I;
int J;
//
TCbDoubleBridgeBase_Candidates() {}
TCbDoubleBridgeBase_Candidates(int AI, int AJ)
{
I=AI;
J=AJ;
};
inline bool operator < (const TCbDoubleBridgeBase_Candidates &other) const
{
int i=I-other.I;
//
if (i<0)
return true;
else if (i==0)
return J<other.J;
else
return false;
}
};
template <typename T>
class CMain
{
public:
CMain();
std::vector<T> data;
};
#ifndef ExplicitInstantiation
#include "sub.cpp"
#endif
#endif //sub_h
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#ifndef sub_cpp
#define sub_cpp
#include "sub.h"
#include <algorithm>
template <typename T>
CMain<T>::CMain()
{
data.reserve(100);
std::sort(data.begin(), data.end(),
[] (TCbDoubleBridgeBase_Candidates lp1, TCbDoubleBridgeBase_Candidates lp2)
{
return lp1<lp2;
});
}
#ifdef ExplicitInstantiation
template class std::vector<TCbDoubleBridgeBase_Candidates>;
template class CMain<TCbDoubleBridgeBase_Candidates>;
#endif
#endif //sub_cpp
|
Now with 3 commands
g++ -c sub.cpp -std=gnu++11
g++ -c ex.cpp -std=gnu++11
g++ sub.o ex.o -std=gnu++11
|
everything is OK. But with next 3 commands:
g++ -c sub.cpp -std=gnu++11 -fno-implicit-templates -DExplicitInstantiation
g++ -c ex.cpp -std=gnu++11 -fno-implicit-templates -DExplicitInstantiation
g++ sub.o ex.o -std=gnu++11 -fno-implicit-templates -DExplicitInstantiation
|
i got linker errors. So how to create instantiation with encapsulation?
Last edited on
anyone has same?
Don't know - not tried it yet.
Topic archived. No new replies allowed.