-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: "Generic" fixed buffer with different sizes #2393
Comments
@orthoxerox |
Seems like template meta-programming in C++: Which will make C# be able to do a lot of fun but useless things haha. Why not put buffer size in its constructor parameters? struct A<T> where T: new()
{
public A(int size) { buffer = new T[size]; }
T[] buffer;
};
class B
{
public B(int size) { field = new A<long>(size); }
A<long> field;
};
int main()
{
A<uint> a = new A<uint>(10);
A<object> a1 = new A<object>(20);
var b = new B(15);
} |
@hez2010 |
This would involve quite a few CLR changes for what seems to be limited benefit. Every version of the constructed generic with a different constant would be incompatible with the next, and that would lead to massive metadata bloat at runtime. With I'm also pretty sure that this has been requested before, I'll see if I can find it. |
I thought that there was a proposal that specifically combined the idea of |
There is my own proposal for fixed-sized buffer types: #78, which formed the basis for the existing championed proposal: #1314 There has also been discussion about extending that further to allow "value arrays", but I don't believe there has been a championed issue yet. One of the concerns here is the metadata bloat required to support arbitrary However, there are some limitations with the generic approach, such as not working with pointers or ref structs; where-as the attribute based approach would (as well as working with marshalling; although that is addressable for the generic form as well). |
Sounds interesting, but it looks like the size of the buffer is still a constant defined at compile time? The only mention of a |
Yes, I don't think you can reliably have dynamically sized "value arrays" that escape the scope of the current method. That is, the space needs to be allocated at the scope of highest use; and if it's dynamic, that size also needs to be determined at the same scope. So, you either need to have a constant size, or manually allocate the space and pass the
That I'm unsure of; it didn't go into much further detail and given that it still requires a compile time constant, but also doesn't work in all scenarios; I think it is much more limiting than the attribute based approach. |
Your example isn't using fixed buffers but a 1D array with element type T. Also fixed buffers currently require the bounds arg to be a compile time constant. |
How about such as following? using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Size = 1)] public struct __1 {}
[StructLayout(LayoutKind.Sequential, Size = 2)] public struct __2 {}
[StructLayout(LayoutKind.Sequential, Size = 3)] public struct __3 {}
[StructLayout(LayoutKind.Sequential, Size = 4)] public struct __4 {}
[StructLayout(LayoutKind.Sequential, Size = 5)] public struct __5 {}
[StructLayout(LayoutKind.Sequential, Size = 6)] public struct __6 {}
[StructLayout(LayoutKind.Sequential, Size = 7)] public struct __7 {}
[StructLayout(LayoutKind.Sequential, Size = 8)] public struct __8 {}
// :
// : using System;
using System.Runtime.CompilerServices;
public struct Sample<N> where N : unmanaged
{
public unsafe struct __Buffer
{
private N _Value;
public ref byte this[int index]
=> ref ((byte*)Unsafe.AsPointer(ref _Value))[index];
}
private __Buffer FixedFieldLike;
// You can access it as if fixed size buffer.
public ref byte this[int index]
{
get
{
if((uint)Unsafe.SizeOf<N>() < (uint)index)
throw new ArgumentOutOfRangeException();
return ref FixedFieldLike[index];
}
}
}
public static class Program
{
public static void Main()
{
var sample = new Sample<__7>();
// :
// :
}
} This can be run on current runtime. |
similiar to #749 |
Earlier there was an issue expressed about the potentially large number of classes this would create. Also, it appears people are doing this anyway, by hand. If that's a big concern, add a compiler warning "more than 100 specialisations of generic X detected, constant may not be constant" |
@AartBluestoke |
Example:
The text was updated successfully, but these errors were encountered: