0

Possible Duplicate:
Why can templates only be implemented in the header file?
Why should the implementation and the declaration of a template class be in the same header file?

I'm a computer science student at some university and we were given files to work on for hw. And I wasn't sure how this kind of instantiation worked.

long code short it looked something like this.

in List.h

#ifndef _LIST_H_
#define _LIST_H_

#include <iterator>
#include <ostream>

template <class T>
class List

/* implementation below but not relevant to this post */
.
.
.
.

....the last few lines of the file below.

#include "list.cpp"

#include "list_given.cpp"

#endif

and List.cpp didn't include List.h

I don't understand how including List.cpp in the header file works.

Community
  • 1
  • 1
wayfare
  • 699
  • 2
  • 9
  • 17

3 Answers3

2

#include just causes textual substitution, nothing else, so it's as if the entire contents of list.cpp were duplicated in the header file. This has nothing to do with "explicit template instantiation".

list.cpp doesn't include the header because otherwise the header would include itself recursively.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 1
    It has everything to do with template instantiation, but there is no *explicit* template instantiation here. – Ben Voigt Feb 26 '12 at 19:48
  • 2
    It wouldn't lead to infinite recursion, since there's a sentinel define in the .h file. – Mr Lister Feb 26 '12 at 19:57
  • So this would be the same thing as writing the entire list.cpp content below the implementation? If so, would this be qualified as implicit instantiation since both the definition and declaration are technically in the same header file? – wayfare Feb 26 '12 at 20:23
  • @wayfare: nothing is being instantiated anywhere in the part of the code that you show. – Fred Foo Feb 26 '12 at 20:36
2

No, nothing is instantiated here, this is simply a solution to the problem that the complete definition of a C++ template must be visible in each translation unit in which the template is instantiated and some programmers have the desire to keep the definition and declaration separate. If this is really worth it, is not commonly accepted. Some people also like to use a different file ending for implementation files that are meant to be included in headers (e.g tcc, icc).

How does it work:

// Foo.h
#ifndef FOO_HEADER_INCLUDED
#define FOO_HEADER_INCLUDED
template<T>
struct Foo {
  void bar();
};

#include "Foo.cpp"
#endif // header guard

// Foo.cpp
// NO HEADER GUARD, NEVER INCLUDE DIRECTLY
// actually this could have an include guard and still work
template<typename T>
void Foo<T>::bar() { /* smart code */ }

Now, every user (someone who includes Foo.h) also includes the implementation file and our template works. An alternative is to simply write the definition in the header file.

pmr
  • 58,701
  • 10
  • 113
  • 156
1

Ahhhh! Don't include .cpp files!

For writing a template class, you usually put most if not all of the implementation in the .h file, so you may not even need a list.cpp. I don't know what list_given.cpp is for, so I can't tell you if you need that one.

When you create a template class, it is just that - a template that tells the compiler how to create a class. So you will need to include list.h containing the whole definition of the template in any file that uses the list class.

Your example doesn't qualify as explicit instantiation - I don't know why you'd need it here. See this question, in particular the accepted answer, for an example of that.

Community
  • 1
  • 1
parkovski
  • 1,503
  • 10
  • 13
  • Sure, but for someone who is just learning C++, unless you have a good reason not to, it's a good idea to stick to accepted practices. Seeing a header file include a source file looks strange and makes you wonder what their reasons are. – parkovski Feb 26 '12 at 19:59
  • @pmr: Putting the implementation in a separate file and `#include`ing it is fairly common. Giving that file a `.cpp` extension is, however, a terrible idea. – Ben Voigt Feb 26 '12 at 20:02
  • If we were talking about something really arcane here, I would agree, but this is a very common thing to do. – pmr Feb 26 '12 at 20:02
  • @BenVoigt I've pointed out in my answer that people like to choose other extensions. You are certainly right, that other extensions than `cpp` are more appropriate, but I've never seen people to agree on one :) – pmr Feb 26 '12 at 20:03
  • @pmr: Oh sure, lots of different extensions can reasonably be used. But not `.C`, `.cpp`, `.cxx`, `.c++` since those all have meaning to `make`. – Ben Voigt Feb 26 '12 at 20:05