Template member function error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tavianator
    New Member
    • Dec 2006
    • 38

    Template member function error

    G++ 4.2.2 reports an error with this innocuous-looking code. Any ideas?

    Code:
    template <typename T>
    class foo
    {
    public:
      template <typename U> void bar() { }
    };
    
    template <typename T>
    void baz() {
      foo<T> f;
      f.bar<T>();
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Apparently, the code is so innocuous that Visual Studio.NET can't find anyhrting wrong with it. I got a compile and link:
    [code=cpp]
    template <typename T>
    class foo
    {
    public:
    template <typename U> void bar() { }
    };

    template <typename T>
    void baz() {
    foo<T> f;
    f.bar<T>();
    }
    int main()
    {
    baz<int>();
    };
    [/code]

    Comment

    • mschenkelberg
      New Member
      • Jun 2007
      • 44

      #3
      gnu compiler wants you to only have declarations in the header file and all implementation in the .cpp file when using templates.

      Max

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by mschenkelberg
        gnu compiler wants you to only have declarations in the header file and all implementation in the .cpp file when using templates.
        Really?? That's odd since templates do not have declarations unless they are export templates. Are you saying that gnu supports export templates?? That would be a real trick since I know of no compilers that do that yet.

        Comment

        • tavianator
          New Member
          • Dec 2006
          • 38

          #5
          It can't possibly matter what goes in a header file and what doesn't, can it? #including happens in preprocessing, before compilation anyway. To be thorough, I tried it that way anyway, and it still doesn't work. It sounds like a compiler bug. Luckily I have a workaround for now.

          Comment

          • tavianator
            New Member
            • Dec 2006
            • 38

            #6
            Aha, silly me, I forgot that dependant member templates need to be prefixed with "template" just like how dependant names have to be prefaced with "typename". This should work (haven't tried it, though):

            Code:
            template <typename T>
            class foo
            {
            public:
              template <typename U> void bar() { }
            };
            
            template <typename T>
            void baz() {
              foo<T> f;
              f.template bar<T>();
            }

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by tavainator
              It can't possibly matter what goes in a header file and what doesn't, can it?
              Yes, it can matter. It can cause your builds to crash in the link with redefinition errors.

              Case in point: Putting variables or coding functions in a header file.

              Every time you include a header with definitions in it (creating variables, coding functions, or doing anything that requires memory) you get a copy of the items defined in the header. If the header is included in several source files that are partof the same program, you get one set of the variables and functions in each object file. When these object files converge on the linker, you get ejected onto the sidewalk for duplucate definitions.

              Header files are to contain declarations only:
              1) function prototypes
              2) templates
              3) enums
              4) struct/class/union
              5) inline functions
              6) macros (if C. If C++ you don't use macros)

              Comment

              Working...