template class and default function parameter

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

    #1

    template class and default function parameter

    [Repost to comp.lang.c++]

    Hi,

    Is this valid C++? It compiles on VC8 and g++ 4.1.1, but fails on Sun
    CC 5.8.

    --cut --
    template<class T>
    class TClass
    {
    public:
    void f(int x = T::EnumID);

    };

    class Unknown;

    class X
    {
    public:
    TClass<Unknownm ;
    };
    -- cut --

    The error message of Sun CC is:

    -- cut --
    "enumid.cc" , line 5: Error: EnumID is not a member of Unknown.
    "enumid.cc" , line 13: Where: While specializing "TClass<Unknown >".
    "enumid.cc" , line 13: Where: Specialized in non-template code.
    1 Error(s) detected.
    -- cut --

    Defining class Unknown before class X makes it compile on Sun CC. I am
    wondering which compiler is correct?

    Qingning

  • Kai-Uwe Bux

    #2
    Re: template class and default function parameter

    Qingning Huo wrote:
    [Repost to comp.lang.c++]
    >
    Hi,
    >
    Is this valid C++? It compiles on VC8 and g++ 4.1.1, but fails on Sun
    CC 5.8.
    >
    --cut --
    template<class T>
    class TClass
    {
    public:
    void f(int x = T::EnumID);
    >
    };
    >
    class Unknown;
    >
    class X
    {
    public:
    TClass<Unknownm ;
    };
    -- cut --
    Whether this is legal or not depends on how you continue. This is legal:


    template<class T>
    class TClass {
    public:
    void f(int x = T::EnumID);
    };

    class Unknown;

    class X {
    public:
    TClass<Unknownm ;
    };

    struct Unknown {
    static int const EnumID=0;
    };

    void dummy ( X x ) {
    x.m.f();
    }

    int main ( void ) {}


    This is not:

    template<class T>
    class TClass {
    public:
    void f(int x = T::EnumID);
    };

    class Unknown;

    class X {
    public:
    TClass<Unknownm ;
    };

    void dummy ( X x ) {
    x.m.f();
    }

    struct Unknown {
    static int const EnumID=0;
    };

    int main ( void ) {}



    I think, what is important is the point of instantiation of the member
    function x.m.f(). The definition of Unknown has to be known at that point.


    Best

    Kai-Uwe Bux

    Comment

    • Qingning

      #3
      Re: template class and default function parameter

      Kai-Uwe Bux wrote:
      Qingning Huo wrote:
      >
      [Repost to comp.lang.c++]

      Hi,

      Is this valid C++? It compiles on VC8 and g++ 4.1.1, but fails on Sun
      CC 5.8.

      --cut --
      template<class T>
      class TClass
      {
      public:
      void f(int x = T::EnumID);

      };

      class Unknown;

      class X
      {
      public:
      TClass<Unknownm ;
      };
      -- cut --
      >
      Whether this is legal or not depends on how you continue.
      Thanks for your reply. The problem is that CC reports error as soon as
      it sees the definition of "class X". As I understand, adding code
      after the point of error would not work in C++.
      This is legal:
      >
      template<class T>
      class TClass {
      public:
      void f(int x = T::EnumID);
      };
      >
      class Unknown;
      >
      class X {
      public:
      TClass<Unknownm ;
      };
      >
      struct Unknown {
      static int const EnumID=0;
      };
      >
      void dummy ( X x ) {
      x.m.f();
      }
      >
      int main ( void ) {}
      >
      This code generates the same error.
      I think, what is important is the point of instantiation of the member
      function x.m.f(). The definition of Unknown has to be known at that point.
      I agree with you. But what is the point of instantiation of f()? The
      following code declares variable of class X, calls TClass<T>::f(), and
      even defines TClass<T>::f() without defining class Unknown. And it
      still passes g++.

      -- cut --
      template<class T>
      class TClass
      {
      public:
      void f(int i = T::EnumID);
      };

      class Unknown;

      class X
      {
      public:
      TClass<Unknownm ;
      };

      X x1;

      int g(X& x0)
      {
      x0.m.f(1);
      }

      template<class T>
      void TClass<T>::f(in t i)
      {
      i = 0;
      }
      -- cut --

      Regards,
      Qingning

      Comment

      Working...