VStudio9 and template member functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • riccardo.gherardi@gmail.com

    #1

    VStudio9 and template member functions

    Hello.
    The code below results in a linker error when compiled with Visual
    Studio 2008, v9.0.21022.8 RTM.
    The problem disappears when Foo::Bar implementation is moved to the
    header file.
    I've read the relevant template FAQs suggesting to never separate
    definition and declaration of template member functions, but they deal
    with code that fails to compile.
    It's a sintax issue (and therefore my fault) or a compiler quirk?
    Thanks in advance, R

    // --- foo.h ---

    class Foo
    {
    public:
    template<class Tint Bar(int);
    };

    // --- foo.cpp ---

    #include "foo.h"
    template<class Tint Foo::Bar(int)
    {
    return 0;
    }

    // --- main.cpp ---

    #include "foo.h"
    int main()
    {
    Foo f;
    int x = f.Bar<double>(3 );
    }
  • riccardo.gherardi@gmail.com

    #2
    Re: VStudio9 and template member functions



    Thanks.
    The referenced solution, telling the compiler what code to generate,
    works.
    In the aforementioned case, it means adding the following line to
    foo.cpp.

    template int Foo::Bar<double >(int);

    Now I just have to manually keep track of the instantiation I wish to
    use... :b R

    Comment

    • james.fysh@gmail.com

      #3
      Re: VStudio9 and template member functions

      On Aug 18, 4:55 am, "riccardo.ghera ...@gmail.com"
      <riccardo.ghera ...@gmail.comwr ote:
      >
      Thanks.
      The referenced solution, telling the compiler what code to generate,
      works.
      In the aforementioned case, it means adding the following line to
      foo.cpp.
      >
          template int Foo::Bar<double >(int);
      >
      Now I just have to manually keep track of the instantiation I wish to
      use... :b R
      Or you could just define the functionality inside the header file, so
      you don't have to explicitly declare each type you want to use.

      Comment

      • anon

        #4
        Re: VStudio9 and template member functions

        james.fysh@gmai l.com wrote:
        On Aug 18, 4:55 am, "riccardo.ghera ...@gmail.com"
        <riccardo.ghera ...@gmail.comwr ote:
        >Thanks.
        >The referenced solution, telling the compiler what code to generate,
        >works.
        >In the aforementioned case, it means adding the following line to
        >foo.cpp.
        >>
        > template int Foo::Bar<double >(int);
        >>
        >Now I just have to manually keep track of the instantiation I wish to
        >use... :b R
        >
        Or you could just define the functionality inside the header file, so
        you don't have to explicitly declare each type you want to use.
        Or include the cpp file

        Comment

        Working...