By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

Handling templated code

P: n/a
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/view...ion/gdcmItem.h

Oct 27 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
mathieu wrote:
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/view...ion/gdcmItem.h
Just a guess but try replacing 194 with this:

nested.template ReadNested<TSwap>(is);
Oct 27 '07 #2

P: n/a
On 27 oct, 23:02, Gianni Mariani <gi4nos...@marian.wswrote:
mathieu wrote:
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.
....two. AFAIK intel icc supports the export keyword (at least on
*NIX).

ref:
http://www.intel.com/support/perform.../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

Oct 27 '07 #3

P: n/a
On 2007-10-27 21:40, mathieu wrote:
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".

--
Erik Wikström
Oct 28 '07 #4

P: n/a
On Oct 28, 10:54 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-10-27 21:40, mathieu wrote:
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".
It's also much easier to switch to export using this scheme,
when it becomes available.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 28 '07 #5

P: n/a
On Oct 28, 1:54 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-10-27 21:40, mathieu wrote:
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".

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

Nov 7 '07 #6

P: n/a
Erik Wikström wrote:
On 2007-10-27 21:40, mathieu wrote:
>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".
With a conditional include, it also works with compilers that don't
require the template definitions to be included in the header.

--
Ian Collins.
Nov 7 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.