C++ Generics issue
Hi.
I'm having a problem converting some C# to C++/CLI,
which involves generics:
//// C# code (compiles and runs): /////////////////////////
// The constraint requires the generic parameter T
// to be convertable to the type of the derived class: public abstract class MyBaseT<T> where T : MyBaseT<T>
{
public MyBaseT()
{
}
} public class MyDerived : MyBaseT<MyDerived>
{
}
//// (equivalent?) C++/CLI code: //////////////////// generic<typename T> where T : MyBaseT<T>
public ref class MyBaseT abstract
{
public:
MyBaseT()
{
}
}; public ref class MyDerived : MyBaseT<MyDerived>
{
};
/// Compiler error: Error 1 error C3393: syntax error in constraint
clause: 'MyBaseT' is not a type (on the first
line of the C++ class declaration).
The C++ compiler doesn't like the generic constraint. Any ideas ?
I didn't see anything in that link that resolves the problem.
The problem is that C++/CLI will not allow you to specify a generic constraint on a class which refers to that same class (this is allowed in C# of course).
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: VB to Python converter
I don't have solution, just two points:
The problem is that C++ compiler requires forward declaration. However, I didn't find a way to make forward declaration - maybe you can do this.
Maybe it is possible to derive MyBaseT from another abstract class, and use this class in constraint. In this case name of base class is known by compiler.
Thanks to all for the replies.
I also tried to forward-declare the class but that doesn't appear to work.
I suppose this follows the general behavior of the C++ compiler, which unlike C#, requires any reference to a type to be defined/declared ahead of the reference. I also feel it's a notable flaw in compilation of managed code.
That's a good solution Einar!
I still find it strange that there's no direct C++/CLI equivalent for this C# code.
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: VB to Python converter
Brian, do you mean specializations such as
template<typename T>
class Base
{};
template<>
class Base<class Derived>
{
private:
Base();
};
class Derived : Base<Derived>
{};
If so, that's not a problem here. The issue is the constraint, which is declared prior to the class name
generic<typename T> where T : MyBaseT<T>
public ref class MyBaseT abstract
This code yields the error "MyBaseT is not a type" in the constraint clause. In C#, the constraint follows the class name, so it's not an issue there.
I did post a suggestion on what the OP can use as a workaround, but I'm not sure if this is fit for his use. Also, if this problem is "unintentional", a bug report should be opened.