explicit instaniation vs std::vector template how

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
44
45
46
#include <iostream>
#include <vector>
//using namespace std;

struct TCbDoubleBridgeBase_Candidates
{
public:
  int I;
  int J;
  //
  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()
  {
    data.reserve(100);
  }
  std::vector<T> data;
};


int main()
{
  CMain<TCbDoubleBridgeBase_Candidates> cs;
  //
  
}



How to compile this code with option of gcc -fno-implicit-templates?
Last edited on
As i googled everything is worse than i expected:
http://gcc.gnu.org/ml/libstdc++/2006-09/msg00244.html
http://bytes.com/topic/c/answers/572842-explicit-instantiation-templates-any-experience

So, has someone worked code for all that stuff to share with?
Add this somewhere.
template class std::vector<TCbDoubleBridgeBase_Candidates>;

http://www.network-theory.co.uk/docs/gccintro/gccintro_60.html

God help you.
Nope...
Compiling is realy OK (http://ideone.com/NSFHtw),
but not linking. As i said, it's rather difficult issue
To get that code to link a with the -fno-implicit-templates there are
two explicit instantiation that you need to do (although, I think one of then is not necessary. see below...)
The following compiles, links and runs..


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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <vector>
//using namespace std;

struct TCbDoubleBridgeBase_Candidates
{
public:
  int I;
  int J;
  //
  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()
  {
    data.reserve(100);
  }
  std::vector<T> data;
};

//explicit instantiations
template class CMain<TCbDoubleBridgeBase_Candidates>; //the straightforward one - I'm not sure you need this one to be explicit
template class std::vector<TCbDoubleBridgeBase_Candidates>; //you will need this one though..


int main()
{
  
   CMain<TCbDoubleBridgeBase_Candidates> cs;
  
  
}

//You could put the explicit instantiations  here instead of before main if you prefer
Last edited on
Does it work on your computer?
In my one refuses int both cases (4.5.3 under cygwin and 4.7.2 under kubuntu)

output for cygwin:

$ gcc -fno-implicit-templates ex.cpp
/tmp/ccm9LSKx.o:ex.cpp:(.text+0x4a): undefined reference to `std::ios_base::Init::Init()'
/tmp/ccm9LSKx.o:ex.cpp:(.text+0x65): undefined reference to `std::ios_base::Init::~Init()'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld: /tmp/ccm9LSKx.o: bad reloc address 0xd in section `.text$_ZN5CMainI30TCbDoubleBridgeBase_CandidatesED1Ev[CMain<TCbDoubleBridgeBase_Candidates>::~CMain()]'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld: final link failed: Invalid operation
...

Last edited on
Please run g++ instead of gcc.
i do run g++ otherwise even stdc++ wouldn't linked at all
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
#include <iostream>
#include <vector>

struct TCbDoubleBridgeBase_Candidates { /* ... */ } ;

template < typename T > struct CMain
{
  CMain() { data.reserve(100) ; }
  private: std::vector<T> data;
};

// uncomment these if required (link errors)
// template struct std::allocator<TCbDoubleBridgeBase_Candidates> ;
// template struct std::char_traits<char> ;
// template struct std::basic_ios<char> ;

template struct std::vector< TCbDoubleBridgeBase_Candidates > ;
template struct CMain<TCbDoubleBridgeBase_Candidates> ;

template struct std::basic_ostream<char> ;
template struct std::basic_istream<char> ;

int main()
{
  CMain<TCbDoubleBridgeBase_Candidates> cs;

  // use standard stream objects (char)
}
To be honest - the way I would figure out a situation like this would be to:

make sure the thing build completely without the -fno-implicit-templates option.

Then build with the -fno-implicit-templates option.

Any items that suddenly go MIA are the one that you need to
explicitly instantiate.


EDIT
((For the record I tried both G++ 3.4.5 and G++ 4.6.2 using codeblocks on windows
and although they gave sliglty different results, the issue of std::ios didn't come
up - it might well depends on what other baggage your ide/build system automatically includes ))
Last edited on
i probably did smth wrong. Everything works for me now.
Topic archived. No new replies allowed.