How do I specify a fixed-size buffer in C++/CLI?

2020-02-12 09:32发布

问题:

In C#, I can specify a fixed sized buffer using the fixed keyword, like so:

public unsafe struct StructWithFixedBuffer
{
    public fixed char FixedBuffer[128];
}

how would I express the same thing in C++/CLI?

回答1:

One of the C++/CLI developer blogs had code for a template solution to this, I'll try to find a link.

Ahh, found it. It's called inline_array.



回答2:

The C# syntax was added as a way to express the C++ syntax you've know forever. :)

public:
    wchar_t FixedBuffer[128];


回答3:

Quote:

size of the 128 element char array is 256 bytes. Fixed size char buffers always take two bytes per character, regardless of the encoding.

So you want:

struct StructWithFixedBuffer
{
    char FixedBuffer[128*2];
};


标签: c++-cli