[CLI-C++] Using sizeof inside a generic function

M

marco_segurini

Hi,

I like to know if is it possible to use sizeof(T) inside a generic
function.

I don't know why the following function compiles while if I define
ON_LINE_STATEMENT it does not.

generic<typename T>
void CopyArrayFromArray(array<T>^ % vVal, int size
, array<Byte>^ vBytes, int % PosNextValidByte)
{
vVal = gcnew array<T>(size);
pin_ptr<T> ptrDest = &vVal[0];
pin_ptr<Byte> ptrBytes = &vBytes[PosNextValidByte];
#if defined(ONE_LINE_STATEMENT)
const Int32 NumBytesDaCopiare = size * sizeof(T);
#else
Int32 NumBytesDaCopiare = sizeof(T);
NumBytesDaCopiare *= size;
#endif
memcpy(ptrDest, ptrBytes, NumBytesDaCopiare);
PosNextValidByte += NumBytesDaCopiare;
}

TIA.
Marco.
 
I

Ioannis Vranos

marco_segurini said:
Hi,

I like to know if is it possible to use sizeof(T) inside a generic
function.

I don't know why the following function compiles while if I define
ON_LINE_STATEMENT it does not.

generic<typename T>
void CopyArrayFromArray(array<T>^ % vVal, int size
, array<Byte>^ vBytes, int % PosNextValidByte)
{
vVal = gcnew array<T>(size);
pin_ptr<T> ptrDest = &vVal[0];
pin_ptr<Byte> ptrBytes = &vBytes[PosNextValidByte];
#if defined(ONE_LINE_STATEMENT)
const Int32 NumBytesDaCopiare = size * sizeof(T);
#else
Int32 NumBytesDaCopiare = sizeof(T);
NumBytesDaCopiare *= size;
#endif
memcpy(ptrDest, ptrBytes, NumBytesDaCopiare);
PosNextValidByte += NumBytesDaCopiare;
}


The current C++/CLI draft says about sizeof():


"15.4.3 Sizeof

The C++ Standard (§5.3.3/1) has been extended, as follows:

“The sizeof operator shall not be applied to an expression that has
function or incomplete type, or to an enumeration type before all its
enumerators have been declared, or to the parenthesized name of such
types, or to an lvalue that designates a bit-field, or to an expression
that has null type, or to a handle, or to a tracking reference, or to a
ref class."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top