c++/cli compiler oddity

Giganews Newsgroups
Subject: c++/cli compiler oddity
Posted by:  Martin Lafferty (no@spam.thanks)
Date: Sun, 14 Jan 2007

Consider the following wee snippet of C#:

  public interface ITest {
    void Test();
  }

  public abstract class A<T> {
    public abstract T FuncA();
  }

  public abstract class B<T>: A<T> where T : ITest {
    public void FuncB() {
      FuncA().Test();
    }
  }

this compiles fine.

Here is the same thing in C++

  public interface class ITest {
    virtual void Test() = 0;
  };

  generic<typename T>
  public ref class A abstract {
  public:
    virtual T Func() = 0;
  };

  generic<typename T> where T : ITest
  public ref class B abstract : public A<T>  {
  public:
    void FuncB() {
    //error C2039: 'Test' : is not a member of 'System::Object'    
      Func()->Test();
        
    }
  };

This does not compile. The constraint on T seems to be ignored.

if FuncB is written as follows:

  generic<typename T> where T : ITest
  public ref class B abstract : public A<T>  {
  public:
    void FuncB() {
      T x = Func();
      x->Test();
    }
  };

It compiles! Is this expected behaviour?

--

Martin L

Replies