Not sure what it is that I'm doing wrong with templates in this case

Started by
17 comments, last by ysg 11 years ago



PS. this is not a good idea in general, putting the code in the header to be generated at compile time is better than forcing a compile time generation of the entire class.

Yeah, I'd like to avoid that if I can smile.png .



You get a gold star for that. I used to use ".inl" files to separate the class definition in the standard 'header' file from the 'inline' file, but Xcode ignores the '.inl' files and you can't debug through them or set breakpoints, so you have to use 'h' or 'hpp' on that platform. (I'm sure you can customize it, I refuse to do so though, might make things nice for me but others working with the codebase may not have all the customizations) Of course, it is non-standard and as such probably not a great thing to do, but it was nice. I could put the declaration of the interface in an hpp, the inline function definitions in a inl and any non-template parameter specific stuff in a cpp where I used the explicit definition as posted above to make the compiler generate the code.


On a side-note, why do you have "Crossbones+" next to your username?
Advertisement

The approach I use is this (seems like "Method 3"):


// template_file.h

#pragma once

/* YOUR TEMPLATE DECLARATION HERE */

#include <template_file.inline.h>

And definitions:


// template_file.inline.h

#pragma once
#include <template_file.h>

/* DEFINITIONS HERE */

"inline" file is used like a usual .cpp file. This speeds up compilation a lot.

Sadly, I don't remember where I first found this approach.

Edit: you can replace "pragma once" with "#ifndef .., #define ..". Same thing.

On a side-note, why do you have "Crossbones+" next to your username?

It's for people who are contributing to the site (through articles):

[attachment=14713:Screen Shot 2013-04-08 at 7.40.43 AM.png]

I was going to add more to this thread but most of what I would've said has already been said.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

On a side-note, why do you have "Crossbones+" next to your username?

It's for people who are contributing to the site (through articles):

attachicon.gifScreen Shot 2013-04-08 at 7.40.43 AM.png

I was going to add more to this thread but most of what I would've said has already been said.

Sadly though, I have yet to find the time to write my first article... sad.png

Some favourite quotes:Never trust a computer you can't throw out a window.
- Steve Wozniak

The best way to prepare [to be a programmer] is to write programs, and to study great programs that other people have written.
- Bill Gates

There's always one more bug.
- Lubarsky's Law of Cybernetic Entomology

Think? Why think! We have computers to do that for us.
- Jean Rostand

Treat your password like your toothbrush. Don't let anybody else use it, and get a new one every six months.
- Clifford Stoll

To err is human - and to blame it on a computer is even more so.
- Robert Orben

Computing is not about computers any more. It is about living.
- Nicholas Negroponte

The approach I use is this (seems like "Method 3"):


// template_file.h

#pragma once

/* YOUR TEMPLATE DECLARATION HERE */

#include <template_file.inline.h>

And definitions:

// template_file.inline.h

#pragma once
#include <template_file.h>

/* DEFINITIONS HERE */


"inline" file is used like a usual .cpp file. This speeds up compilation a lot.
Sadly, I don't remember where I first found this approach.

Edit: you can replace "pragma once" with "#ifndef .., #define ..". Same thing.


Ok, so you have two header files?

Having a hard time visualizing your approach. The way I understand it is this:
- template-file.hpp: this is where you have the class declaration (and the sub-methods)
- template-file.inline.hpp: this is a just a place-holder file to make it easier for the compiler to swallow
- template-file.cpp: this is where the implementation of the template class is located

Am I right or am I way off?

The approach I use is this (seems like "Method 3"):


// template_file.h

#pragma once

/* YOUR TEMPLATE DECLARATION HERE */

#include <template_file.inline.h>

And definitions:

// template_file.inline.h

#pragma once
#include <template_file.h>

/* DEFINITIONS HERE */


"inline" file is used like a usual .cpp file. This speeds up compilation a lot.
Sadly, I don't remember where I first found this approach.

Edit: you can replace "pragma once" with "#ifndef .., #define ..". Same thing.

For most purposes, this is probably the best way to go. Only one thing:


#include <template_file.h>

This line in "template_file.inline.h" is redundant, unless you include "template_file.inline.h" from somewhere else and are not sure if "template_file.h" will be included beforehand. Otherwise the include guards just do their job. In my opinion, it is cleaner to just say that the "*.inline.h" files are not for use outside of "*.h".

@ysg: There is no "*.cpp". The "#include" directive basically inserts the plain text from the included file verbatim. From the compilers view, everything ends up inside the header. The inline files containing the implementation are just for organization.

The approach I use is this (seems like "Method 3"):


// template_file.h

#pragma once

/* YOUR TEMPLATE DECLARATION HERE */

#include <template_file.inline.h>

And definitions:

// template_file.inline.h

#pragma once
#include <template_file.h>

/* DEFINITIONS HERE */


"inline" file is used like a usual .cpp file. This speeds up compilation a lot.
Sadly, I don't remember where I first found this approach.

Edit: you can replace "pragma once" with "#ifndef .., #define ..". Same thing.

Ok, so you have two header files?

Having a hard time visualizing your approach. The way I understand it is this:
- template-file.hpp: this is where you have the class declaration (and the sub-methods)
- template-file.inline.hpp: this is a just a place-holder file to make it easier for the compiler to swallow
- template-file.cpp: this is where the implementation of the template class is located

Am I right or am I way off?

No, implementation is in template-file.inline.hpp, because there is no .cpp file. Template files are inlined anyways per-use. Or, should I say, compiler chooses how and where to compile multiple binaries resulting from used variations of the template parameters.

You would include template-file.hpp in other files to use template.

For maximum fun, some people have three headers per template class: one containing the class definition without the member function definitions, one containing the member function definitions, and one containing a forward declaration for the template class and any common typedefs involving the template class. The last is useful since template classes can have non-trivial forward declarations.

For maximum fun, some people have three headers per template class: one containing the class definition without the member function definitions, one containing the member function definitions, and one containing a forward declaration for the template class and any common typedefs involving the template class. The last is useful since template classes can have non-trivial forward declarations.

Nah, I'm good with the current level of "fun"/complexity/insanity smile.png .

This topic is closed to new replies.

Advertisement