简体   繁体   English

C#C ++模板的通用行为

[英]C# Generic behavior with C++ Templates

I'm attempting to port some C# code to C++. 我正在尝试将一些C#代码移植到C ++。 I'd like to keep the structure similar, but I realize this might not always be possible. 我想保持结构相似,但我意识到这可能并不总是可行的。

Based on my research, C++ templates are supposed to be more powerful/flexible than C# Generics, yet I can't seem to recreate a particular behavior. 根据我的研究,C ++模板应该比C#Generics更强大/更灵活,但我似乎无法重新创建特定的行为。

Here's an overly simplified example of my current C# structure: 这是我当前C#结构的一个过于简化的例子:

public class MyGeneric<T>
{
    public void MyTypeSafeMethod(T arg1)
    {
        //logic here
    }
}

public class MyImplA : MyGeneric<MyImplA>
{
    //Class config etc..
}

public class MyImplB : MyGeneric<MyImplB>
{
    //Other config etc...
}

public static class MyCollectionOfGenericImpl
{
    public static MyImplA A = new MyImplA();
    public static MyImplB B = new MyImplB();
}

public class Program
{
    void Main()
    {
        //Doesn't compile, method is typesafe 
        MyCollectionOfGenericImpl.A.MyTypeSafeMethod(MyCollectionOfGenericImpl.B);
    }
}

As of right now, I don't really see a way to do this using the C++ templates (did some looking at typedef + templates, but I don't see how that will work for me). 到目前为止,我并没有真正看到使用C ++模板的方法(有些人看过typedef +模板,但我看不出这对我有用)。

Currently, my solution would be to scrap templates completely. 目前,我的解决方案是完全废弃模板。 I would have a base class (ie MyGeneric) with a protected method (ie MyTypeSafeMethodProt) that has my logic. 我将有一个基类(即MyGeneric)与受保护的方法(即MyTypeSafeMethodProt)具有我的逻辑。 I would then expose a new type-safe method in each class implementation that utilizes the protected logic. 然后,我将在每个使用受保护逻辑的类实现中公开一个新的类型安全方法。

Any way to achieve this with templates or another way with less code? 用模板或其他方式用更少的代码实现这一点的任何方法?

There is no problem at all converting the given code to C++. 将给定代码转换为C ++没有任何问题。 It is just a syntactical (mechanical) transformation. 它只是一种语法(机械)转换。 This reproduces the desired compilation error on the call in the main function. 这将在main函数中重现所需的编译错误。

In short, I'm completely baffled as to what the perceived problem is. 简而言之,我对所感受到的问题感到困惑。

But in the hope that it may help, regardless of problem, here's corresponding C++ code with the original C# code's names: 但是,希望它可以帮助,无论问题如何,这里有相应的C ++代码与原始C#代码的名称:

template< class T >
struct MyGeneric
{
    void MyTypeSafeMethod( T& arg1 )
    {
        //logic here
    }
};

struct MyImplA : MyGeneric<MyImplA>
{
    //Class config etc..
};

struct MyImplB : MyGeneric<MyImplB>
{
    //Other config etc...
};

struct MyCollectionOfGenericImpl
{
    static MyImplA A;
    static MyImplB B;
};

MyImplA MyCollectionOfGenericImpl::A;
MyImplB MyCollectionOfGenericImpl::B;

auto main()
    -> int
{
    //Doesn't compile, method is typesafe 
    MyCollectionOfGenericImpl::A.MyTypeSafeMethod( MyCollectionOfGenericImpl::B );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM