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

  • Thread starter Thread starter marco_segurini
  • Start date Start date
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.
 
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."
 
Back
Top