通用构造要求类型“Cell”是非托管类型

问题描述 投票:4回答:1

为什么我不能在F#中使用通用的非托管结构?可能是Cell<'T when 'T: unmanaged>不是不受管理的,那么我怎么能解决这个问题?

type FloatCell =
    struct
        val x: float
        val y: nativeptr<FloatCell>
    end

[<Struct>]
[<StructLayout(LayoutKind.Sequential)>]
type Cell<'T when 'T: unmanaged> =
    struct
        val x: 'T
        val y: nativeptr<Cell<'T>>
    end   

错误FS0001:通用构造要求类型'Cell <'T>'是非托管类型[E:\ dzmitry \ src \ uncorefx \ src \ uncorefx \ uncorefx.fsproj]

更新:

C#也一样。

   unsafe struct FloatCell
    {
        public float val;
        public FloatCell* next;
    }

    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
    unsafe struct Cell<T> where T: unmanaged
    {
        public float val;
        public Cell<T>* next;
    }

有错误:

错误CS0208:无法获取地址,获取大小或声明指向托管类型的指针('Program.Cell')

我不认为它是管理的。

UPDATE2:

我试过属性。没有帮助。我使用了扩展属性进行强制转换。可能解决方案但质疑为什么我不能原生那样做?或者我能做到吗?或者我应该提出C#/ F#问题?

[<Struct>]
[<NativeCppClass>]
[<System.Runtime.CompilerServices.UnsafeValueType>]
[<StructLayout(LayoutKind.Sequential)>]
type Cell<'T when 'T: unmanaged> =
    struct
        val element: 'T
        val next:  voidptr
    end    

type Cell<'T when 'T: unmanaged> with
    member  x.Next = x.next |> NativePtr.ofVoidPtr<'T> 

UPDATE3:

我试图结束指针,并没有指针进入问题。

    public struct UnmanagedStruct
    {
    }

    public struct UnmanagedStructWithSpecifiedGenerics
    {
        public EmptyCell<float> cell;
    }

    public ref struct RefUnmanagedStruct
    {
        public EmptyCell<float> cell;
    }

    public  struct EmptyCell<T> where T : unmanaged
    {
    }

然后实例化:

        var compiles1 = new UnmanagedStructWithSpecifiedGenerics();
        var compiles2 = new EmptyCell<UnmanagedStruct>();
        var CS8377_1 = new EmptyCell<EmptyCell<float>>();
        var CS8377_1 = new EmptyCell<UnmanagedStructWithSpecifiedGenerics>();
        var CS0306 = new EmptyCell<RefUnmanagedStruct>();

导致:

错误CS8377:类型'EmptyCell'必须是不可为空的值类型,以及任何嵌套级别的所有字段,以便在泛型类型或方法'EmptyCell'中将其用作参数'T'

错误CS8377:类型'UnmanagedStructWithSpecifiedGenerics'必须是不可为空的值类型,以及任何嵌套级别的所有字段,以便在泛型类型或方法'EmptyCell'中将其用作参数'T'

错误CS0306:类型'RefUnmanagedStruct'不能用作类型参数

错误信息错误?我应该向Roslyn编译器提出问题吗?

c# .net f# native unmanaged
1个回答
5
投票

它似乎是设计上的,虽然我不确定限制的原因。以下是F# spec的引用:

5.2.9 Unmanaged Constraints

非托管约束具有以下形式:typar : unmanaged

在约束求解(第14.5节)期间,如果type不受管理,则满足约束type : unmanaged,如下所示:

  • 类型sbytebytecharnativeintunativeintfloat32floatint16uint16int32uint32int64uint64decimal是不受管理的。
  • 类型nativeptr<type>是不受管理的。
  • 非泛型结构类型,其字段都是非托管类型,是非托管的。

注意最后一个项目中明确提到了非泛型结构类型。

© www.soinside.com 2019 - 2024. All rights reserved.