为什么Visual C ++不接受具有相同名称的通用和非通用接口? [英] Why Visual C++ not accept generic and non-generic interfaces with the same name?

查看:136
本文介绍了为什么Visual C ++不接受具有相同名称的通用和非通用接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对同名的通用和非通用接口有问题。



我的代码:

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

generic< typename T>公共接口类IPacked:public IPacked {
// Property
property T PackedValue {
virtual T get();
virtual void set(T value);
}
};

我知道,它可以编译。我的朋友在旧团队项目中使用它。
但是,这个问题不存在于函数中。

解决方案

由于C ++语言规则,从 C ++ / CLI语言规范,第31.1章: p>


一个通用类型不得与任何其他通用类型,模板,类,委托,函数,
对象,枚举,枚举器,命名空间或类型(C ++标准3.3),除了在C ++标准的14.5.4中指定的
。除非通用函数可以通过具有相同名称的非通用
函数或者由具有相同名称的其他通用函数重载,在命名空间范围或类范围中声明的通用名称
将是唯一的,因为范围。


C ++标准的第14.5.4节讨论了部分模板特化,没有什么可以帮助你。

作为一种解决方法,您可以考虑在C#程序集中声明接口并添加对它的引用。或者使用不同的命名空间,很像System :: Collections :: IEnumerable vs System :: Collections :: Generic :: IEnumerable<>。


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.

解决方案

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<>.

这篇关于为什么Visual C ++不接受具有相同名称的通用和非通用接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆