Member template function specialization in a template class

Discussion in 'C++' started by Ruben Campos, Feb 14, 2005.

  1. Ruben Campos

    Ruben Campos Guest

    Greetings. Please, take a look to the next code, where I have a problem with
    specialization of class member functions.

    // ########## CPrinter.h ##########
    #ifndef PRINTER_H
    #define PRINTER_H

    enum TPrintPolicy { POLICY1 = 0, POLICY2 };

    template <typename T>
    class CPrinter
    {
    public:
    template <TPrintPolicy P> void Print (T const value);
    };

    #include "CPrinter.cpp"
    #endif

    // ########## CPrinter.cpp ##########
    #include <iostream>
    using std::cout;
    using std::endl;

    template <typename T>
    template <TPrintPolicy P>
    void
    CPrinter <T>::print <P> (T const value)
    {
    cout << "CPrinter <T>::print <P> prints " << value << " value." << endl;
    }

    template <typename T>
    template <>
    void
    CPrinter <T>::print <POLICY1> (T const value)
    {
    cout << "CPrinter <T>::print <POLICY1> prints " << value << " value." <<
    endl;
    }

    // ########## MyClass.h ##########
    #ifndef MY_CLASS_H
    #define MY_CLASS_H

    class MyClass
    {
    public:
    void Foo ();
    };

    #endif

    // ########## MyClass.cpp ##########
    #include "MyClass.h"
    #include "CPrinter.h"

    void
    MyClass::Foo ()
    {
    CPrinter <int> printer;
    printer.Print <POLICY2> (5);
    }

    // ########## main.cpp ##########
    #include "MyClass.h"
    #include "CPrinter.h"

    int
    main (int argn, char ** argv)
    {
    CPrinter <int> printer;
    printer.Print <POLICY1> (4);

    MyClass x;
    x.Foo();

    return 0;
    }

    I've tried this with Microsoft Visual C++ .NET (7.x). If I comment the
    CPrinter <T>::print <POLICY1> specialized implementation (in CPrinter.cpp),
    this source builds and runs fine. However, compiler returns errors when
    including that specialization of CPrinter <T>::print member function. As a
    remarkable note, I've noticed that the number of errors returned by the
    compiler increases when changing the order of the two #include in the
    main.cpp file, with new "MyClass not defined" like errors.

    Could someone say me what is happening here? Is there any right way to
    specialize the template CPrinter <T>::print method?

    Thank you vey much in advance.
    Ruben Campos, Feb 14, 2005
    #1
    1. Advertising

  2. Ruben Campos

    rasbury Guest

    "Ruben Campos" <> wrote in message
    news:cuq5ou$nlu$...
    > // ########## CPrinter.h ##########
    > #ifndef PRINTER_H
    > #define PRINTER_H
    >
    > enum TPrintPolicy { POLICY1 = 0, POLICY2 };
    >
    > template <typename T>
    > class CPrinter
    > {
    > public:
    > template <TPrintPolicy P> void Print (T const value);
    > };
    >
    > #include "CPrinter.cpp"
    > #endif
    >
    > // ########## CPrinter.cpp ##########
    > #include <iostream>
    > using std::cout;
    > using std::endl;
    >
    > template <typename T>
    > template <TPrintPolicy P>
    > void
    > CPrinter <T>::print <P> (T const value)
    > {
    > cout << "CPrinter <T>::print <P> prints " << value << " value." <<
    > endl;
    > }
    >
    > template <typename T>
    > template <>
    > void
    > CPrinter <T>::print <POLICY1> (T const value)
    > {
    > cout << "CPrinter <T>::print <POLICY1> prints " << value << " value."
    > << endl;
    > }
    >
    > // ########## MyClass.h ##########
    > #ifndef MY_CLASS_H
    > #define MY_CLASS_H
    >
    > class MyClass
    > {
    > public:
    > void Foo ();
    > };
    >
    > #endif
    >
    > // ########## MyClass.cpp ##########
    > #include "MyClass.h"
    > #include "CPrinter.h"
    >
    > void
    > MyClass::Foo ()
    > {
    > CPrinter <int> printer;
    > printer.Print <POLICY2> (5);
    > }
    >
    > // ########## main.cpp ##########
    > #include "MyClass.h"
    > #include "CPrinter.h"
    >
    > int
    > main (int argn, char ** argv)
    > {
    > CPrinter <int> printer;
    > printer.Print <POLICY1> (4);
    >
    > MyClass x;
    > x.Foo();
    >
    > return 0;
    > }
    >
    > I've tried this with Microsoft Visual C++ .NET (7.x). If I comment the
    > CPrinter <T>::print <POLICY1> specialized implementation (in
    > CPrinter.cpp), this source builds and runs fine. However, compiler returns
    > errors when including that specialization of CPrinter <T>::print member
    > function. As a remarkable note, I've noticed that the number of errors
    > returned by the compiler increases when changing the order of the two
    > #include in the main.cpp file, with new "MyClass not defined" like errors.
    >
    > Could someone say me what is happening here? Is there any right way to
    > specialize the template CPrinter <T>::print method?


    One of the non-standards conforming issues with VC++ .NET is that it
    requires template members to be defined within the template, inline style
    (notes on standards conformance can be found in the documentation under
    Visual C++ -> Visual C++ Reference -> C/C++ Languages -> C++ Language
    Reference -> Standard Compliance Issues in Visual C++). Try moving the
    definitions of the function and its specialisation into the class
    definition. It is possible that the dependency of the errors on the order of
    the #includes is to do with the compiler skipping the declaration of MyClass
    in attempting to recover from errors raised in CPrinter.h (while compiling
    main.cpp). HTH,

    Richard Asbury
    rasbury, Feb 14, 2005
    #2
    1. Advertising

  3. Ruben Campos

    red floyd Guest

    rasbury wrote:
    > [redacted]


    That's only in VC.NET 2002 (7.0). 7.1's docs say that it's been fixed.
    red floyd, Feb 14, 2005
    #3
  4. Ruben Campos

    Guest

    , Feb 15, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    4
    Views:
    7,645
    pdixtl
    Jun 4, 2010
  2. Bjoern Knafla
    Replies:
    2
    Views:
    3,816
    Bjoern Knafla
    Feb 16, 2005
  3. BigMan
    Replies:
    1
    Views:
    518
  4. Joseph Turian
    Replies:
    4
    Views:
    566
    John Carson
    Mar 20, 2006
  5. toton
    Replies:
    2
    Views:
    320
    toton
    Feb 22, 2008
Loading...

Share This Page