为什么我不能在F#中使用通用的非托管结构?可能是Cell<'T当'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’)
我不认为它是管理的.
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>();
导致:
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,net,f,native,unmanaged 来源: https://codeday.me/bug/20190607/1194196.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。