Handling templated code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mathieu

    #1

    Handling templated code

    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/
    DataStructureAn dEncodingDefini tion/gdcmItem.h:149: error: expected
    primary-expression before '>' token

    Download Grassroots DICOM for free. Cross-platform DICOM implementation. Grassroots DiCoM is a C++ library for DICOM medical files. It is accessible from Python, C#, Java and PHP.


  • Gianni Mariani

    #2
    Re: Handling templated code

    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/
    DataStructureAn dEncodingDefini tion/gdcmItem.h:149: error: expected
    primary-expression before '>' token
    >
    Download Grassroots DICOM for free. Cross-platform DICOM implementation. Grassroots DiCoM is a C++ library for DICOM medical files. It is accessible from Python, C#, Java and PHP.

    >
    Just a guess but try replacing 194 with this:

    nested.template ReadNested<TSwa p>(is);

    Comment

    • mathieu

      #3
      Re: Handling templated code

      On 27 oct, 23:02, Gianni Mariani <gi4nos...@mari an.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<TSwa p>(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

      Comment

      • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

        #4
        Re: Handling templated code

        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

        Comment

        • James Kanze

          #5
          Re: Handling templated code

          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:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          • Pixel.to.life

            #6
            Re: Handling templated code

            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.

            Comment

            • Ian Collins

              #7
              Re: Handling templated code

              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.

              Comment

              Working...