如何在隐式运算符中使用泛型?

发布于 2024-07-17 05:16:51 字数 670 浏览 3 评论 0原文

我有一个非常简单的 C++ 类:

struct Pt_t
{
    T x, y;
    template <class T2> operator Pt_t<T2>() { Pt_t<T2> pt = {x, y}; return pt; }
};

它允许我创建一个具有 T 作为我想要的任何类型的 pt。 我也可以Pt_t= Pt_t; 没有问题。 我如何在 C# 中做同样的事情? 我尝试了以下方法并收到错误:

    class Pt<T>
    {
        public T x, y;
        //between operator and <T2>, error CS1031: Type expected
        public static implicit operator<T2> Pt<T>(Pt<T2> v) {
            Pt<T> r = new Pt<T>();
            r.x = v.x;
            r.y = v.y;
            return r; 
        }
    }

I have a c++ class that is very simple:

struct Pt_t
{
    T x, y;
    template <class T2> operator Pt_t<T2>() { Pt_t<T2> pt = {x, y}; return pt; }
};

That allows me to create a pt that has T as any type I want. I can also do Pt_t<s8> = Pt_t<u64>; without a problem. How do I do the same in C#? I tried the below and got an error:

    class Pt<T>
    {
        public T x, y;
        //between operator and <T2>, error CS1031: Type expected
        public static implicit operator<T2> Pt<T>(Pt<T2> v) {
            Pt<T> r = new Pt<T>();
            r.x = v.x;
            r.y = v.y;
            return r; 
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕头说它不想醒 2024-07-24 05:16:51

不,我认为这是不可能的。 您可能需要添加一个方法,例如 To

下一个问题将是“如何从 T2T - 你不能只分配它们。一个选项可能是转换委托:

public Pt<TDestination> To<TDestination>(
    Converter<T, TDestination> converter)
{
    if (converter == null) throw new ArgumentNullException("converter");
    Pt<TDestination> t = new Pt<TDestination>();
    t.x = converter(x);
    t.y = converter(y);
    return t;
}
...
var p = new Pt<int> { x = 1, y = 2 };
var p2 = p.To(t => t.ToString()); // a Pt<string>

No, I don't think that is possible. You may have to add a method, such as To<T>.

The next problem will be "how to get from T2 to T - you can't just assign them. One option might be a conversion delegate:

public Pt<TDestination> To<TDestination>(
    Converter<T, TDestination> converter)
{
    if (converter == null) throw new ArgumentNullException("converter");
    Pt<TDestination> t = new Pt<TDestination>();
    t.x = converter(x);
    t.y = converter(y);
    return t;
}
...
var p = new Pt<int> { x = 1, y = 2 };
var p2 = p.To(t => t.ToString()); // a Pt<string>
迎风吟唱 2024-07-24 05:16:51

您可以使用 Nemerle: ( http://github.com/rsdn/nemerle ) :

using System.Console;

class A[T]
{
    public static @:[From](x : A[From]) : A[T]
    {
      A()
    }
}


class Base{}
class Derived{}


def a = A.[Derived]();
def b : A[Base] = a;

WriteLine($"$a $b");

输出:

A`1[Derived] A`1[Base]

Reflector代码:

internal class A<T>
{
    public static implicit operator A<T><From>(A<From> x)
    {
        return new A<T>();
    }
}

public class a
{
    public static void Main()
    {
        A<Derived> a = new A<Derived>();
        A<Base> a2 = (A<Base>) a;
        Console.WriteLine(Convert.ToString(a) + " " + Convert.ToString(a2));
    }
}

You can use Nemerle: ( http://github.com/rsdn/nemerle ) :

using System.Console;

class A[T]
{
    public static @:[From](x : A[From]) : A[T]
    {
      A()
    }
}


class Base{}
class Derived{}


def a = A.[Derived]();
def b : A[Base] = a;

WriteLine($"$a $b");

Output:

A`1[Derived] A`1[Base]

Reflector for code:

internal class A<T>
{
    public static implicit operator A<T><From>(A<From> x)
    {
        return new A<T>();
    }
}

public class a
{
    public static void Main()
    {
        A<Derived> a = new A<Derived>();
        A<Base> a2 = (A<Base>) a;
        Console.WriteLine(Convert.ToString(a) + " " + Convert.ToString(a2));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文