Maciej Klemarczyk
Maciej Klemarczyk

Reputation: 197

Why Visual C++ not accept generic and non-generic interfaces with the same name?

I have problem with generic and non-generic interfaces with the same name.

My code:

public interface class IPacked {
    // Methods
    void PackFromDouble(double realNumber);
    double ToDouble( );
};

generic<typename T> public interface class IPacked : public IPacked {
    // Property
    property T PackedValue {
        virtual T get( );
        virtual void set(T value);
    }
};

I know that, it can be compiled. My friend use it in old team project. However, this problem does not exist in the functions.

Upvotes: 2

Views: 286

Answers (1)

Hans Passant
Hans Passant

Reputation: 942010

It is not supported because of C++ language rules. From the C++/CLI Language Specification, chapter 31.1:

A generic type shall not have the same name as any other generic type, template, class, delegate, function, object, enumeration, enumerator, namespace, or type in the same scope (C++ Standard 3.3), except as specified in 14.5.4 of the C++ Standard. Except that a generic function can be overloaded either by nongeneric functions with the same name or by other generic functions with the same name, a generic name declared in namespace scope or in class scope shall be unique in that scope.

Chapter 14.5.4 of the C++ Standard talks about partial template specializations, nothing that will help you here.

As a workaround, you could consider declaring the interfaces in a C# assembly and adding a reference to it. Or using different namespaces, much like System::Collections::IEnumerable vs System::Collections::Generic::IEnumerable<>.

Upvotes: 3

Related Questions