gpt4 book ai didi

c# - 泛型构造要求类型 'Cell<' T>' 是非托管类型

转载 作者:行者123 更新时间:2023-11-30 15:15:20 25 4
gpt4 key购买 nike

为什么我不能在 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

给予

error FS0001: A generic construct requires that the type 'Cell<'T>' is an unmanaged type [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;
}

错误:

error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('Program.Cell')

我不认为它是被管理的。

更新 2:

我试过属性。没有帮助。我已经使用扩展属性进行转换。可能的解决方案。但是质疑为什么我不能在本地做到这一点?或者我能做什么?还是我应该提出 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>

更新 3:

我试图总结指点,并在没有指点的情况下进入问题。

    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>();

导致:

error CS8377: The type 'EmptyCell' must be a non-nullable value type, along with all fields at any level of nesting, in order to use it as parameter 'T' in the generic type or method 'EmptyCell'

error CS8377: The type 'UnmanagedStructWithSpecifiedGenerics' must be a non-nullable value type, along with all fields at any level of nesting, in order to use it as parameter 'T' in the generic type or method 'EmptyCell'

error CS0306: The type 'RefUnmanagedStruct' may not be used as a type argument

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

最佳答案

这似乎是设计使然,但我不确定限制的原因。这是来自 F# spec 的引述:

5.2.9 Unmanaged Constraints

An unmanaged constraint has the following form: typar : unmanaged

During constraint solving (§14.5), the constraint type : unmanaged is met if type is unmanaged as specified below:

  • Types sbyte, byte, char, nativeint, unativeint, float32, float, int16, uint16, int32, uint32, int64, uint64, decimal are unmanaged.
  • Type nativeptr<type> is unmanaged.
  • A non-generic struct type whose fields are all unmanaged types is unmanaged.

注意非泛型结构类型在最后一个项目符号中明确提及。

关于c# - 泛型构造要求类型 'Cell<' T>' 是非托管类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52228109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com