Handling templated code

M

mathieu

Hi there,

I am currently struggling with a compiler error on a templated code.
The error is complete non-sense. I think the issue here is that I am
including files in a loop. To handle templates I usually have 3 types
of files:
- .h : interface + inline function
- .cxx : implementation (no templates)
- .txx: implementation of templates (with #if blocker to prevent
multiple inclusion)

The problem is that I often struggle : when to include those txx
files ? As a rule of thumb I try to include them only once in some
central .cxx file. I have been searching the C++ FAQ for such rules of
thumbs. Are any good practices to follow, I have been browsing through
the STL of gcc and they manage to implement everything in the
interface file.

I can reproduce the issue with gcc 3.x / gcc 4.x and visual studio
2005 and they all give different errors...but on the same line :)

Thanks,
-Mathieu

For reference here is the gcc 3.x error:

/home/mmalaterre/Projects/gdcm/trunk/Source/
DataStructureAndEncodingDefinition/gdcmItem.h:149: error: expected
primary-expression before '>' token

http://gdcm.svn.sourceforge.net/vie...DataStructureAndEncodingDefinition/gdcmItem.h
 
G

Gianni Mariani

mathieu said:
Hi there,

I am currently struggling with a compiler error on a templated code.
The error is complete non-sense. I think the issue here is that I am
including files in a loop. To handle templates I usually have 3 types
of files:
- .h : interface + inline function
- .cxx : implementation (no templates)
- .txx: implementation of templates (with #if blocker to prevent
multiple inclusion)

The problem is that I often struggle : when to include those txx
files ?

Kind of pointless. I place all the template code in the .h file. If
the compiler supports the "export" command then it may work but since
only one commercial (Comeau) compiler does so, you're outa luck.
... As a rule of thumb I try to include them only once in some
central .cxx file. I have been searching the C++ FAQ for such rules of
thumbs. Are any good practices to follow, I have been browsing through
the STL of gcc and they manage to implement everything in the
interface file.

I can reproduce the issue with gcc 3.x / gcc 4.x and visual studio
2005 and they all give different errors...but on the same line :)

Thanks,
-Mathieu

For reference here is the gcc 3.x error:

/home/mmalaterre/Projects/gdcm/trunk/Source/
DataStructureAndEncodingDefinition/gdcmItem.h:149: error: expected
primary-expression before '>' token

http://gdcm.svn.sourceforge.net/vie...DataStructureAndEncodingDefinition/gdcmItem.h

Just a guess but try replacing 194 with this:

nested.template ReadNested<TSwap>(is);
 
M

mathieu

Kind of pointless. I place all the template code in the .h file. If
the compiler supports the "export" command then it may work but since
only one commercial (Comeau) compiler does so, you're outa luck.

....two. AFAIK intel icc supports the export keyword (at least on
*NIX).

ref:
http://www.intel.com/support/performancetools/c/sb/cs-015003.htm

....
The export keyword for templates is supported in Intel® C++ Compiler
for Linux* 8.1 or newer. It is supported in the Intel® C++ Compiler
for Mac OS*. But it is not supported in the Intel® C++ Compiler for
Windows*.
....
Just a guess but try replacing 194 with this:

nested.template ReadNested<TSwap>(is);

Hum... did the trick. I guess my class is templated and the function
is too... I don't remember I had to do it this way.

Thanks a bunch you saved me a lot of troubles,
-Mathieu
 
G

Guest

Hi there,

I am currently struggling with a compiler error on a templated code.
The error is complete non-sense. I think the issue here is that I am
including files in a loop. To handle templates I usually have 3 types
of files:
- .h : interface + inline function
- .cxx : implementation (no templates)
- .txx: implementation of templates (with #if blocker to prevent
multiple inclusion)

The problem is that I often struggle : when to include those txx
files ?

I usually just put it all in the .h file, but I recently I tried a
similar scheme with the implementation in .hpp files. But with the twist
that I included the .hpp file at the end of the .h file (which gives the
same effect as if it was all in the .h files but made the .h files a bit
"cleaner".
 
J

James Kanze

On 2007-10-27 21:40, mathieu wrote:
I usually just put it all in the .h file, but I recently I tried a
similar scheme with the implementation in .hpp files. But with the twist
that I included the .hpp file at the end of the .h file (which gives the
same effect as if it was all in the .h files but made the .h files a bit
"cleaner".

It's also much easier to switch to export using this scheme,
when it becomes available.
 
P

Pixel.to.life

I usually just put it all in the .h file, but I recently I tried a
similar scheme with the implementation in .hpp files. But with the twist
that I included the .hpp file at the end of the .h file (which gives the
same effect as if it was all in the .h files but made the .h files a bit
"cleaner".

I have seen the usage that Erik mentioned. It seems to have worked
till now. It is clean, and it works.
 
I

Ian Collins

Erik said:
I usually just put it all in the .h file, but I recently I tried a
similar scheme with the implementation in .hpp files. But with the twist
that I included the .hpp file at the end of the .h file (which gives the
same effect as if it was all in the .h files but made the .h files a bit
"cleaner".
With a conditional include, it also works with compilers that don't
require the template definitions to be included in the header.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top