Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer of generic type

Why is the use of pointers to generic types invalid in C#? int?* is invalid, whereas typeof(int?).MakePointerType() doesn't produce an exception.

According to MSDN, a pointer can be of:

sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool.

Any enum type.

Any pointer type.

Any user-defined struct type that contains fields of unmanaged types only.

I don't see any limitation relating to generics. int? looks valid, as it contains only a bool and int field.

like image 572
IS4 Avatar asked Jun 05 '15 21:06

IS4


People also ask

What is genetic pointer?

They are born obsessed with finding birds. When they find one they stalk it like a cat. When close enough they take up a “pointing” position that never varies, with one paw raised and the tail horizontal. The only explanation is that the behaviour is passed on by the genes.

What are generic pointer explain with example?

The void pointer in C is a pointer which is not associated with any data types. It points to some data location in the storage means points to the address of variables. It is also called general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

What is genetic pointer in C++?

The keyword void is used as the return type of a function not returning a value and to indicate an empty argument list to a function. More important in C++, however, is the use of void* as a generic pointer type.

What is the generic pointer that can point to objects of any data type?

A void pointer is a pointer that can point to any type of object, but does not know what type of object it points to. A void pointer must be explicitly cast into another type of pointer to perform indirection. A null pointer is a pointer that does not point to an address. A void pointer can be a null pointer.


1 Answers

To quote the C# 5.0 specification §18.2 Pointer Types

Unlike references (values of reference types), pointers are not tracked by the garbage collector—the garbage collector has no knowledge of pointers and the data to which they point. For this reason a pointer is not permitted to point to a reference or to a struct that contains references, and the referent type of a pointer must be an unmanaged-type.

An unmanaged-type is any type that isn’t a reference-type or constructed type, and doesn’t contain reference-type or constructed type fields at any level of nesting. In other words, an unmanaged-type is one of the following:

  • sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool.
  • Any enum-type.
  • Any pointer-type.
  • Any user-defined struct-type that is not a constructed type and contains fields of unmanaged-types only.

The key section that is stopping you is a the constructed type restriction.

from §1.6.3 Type parameters (emphasis mine)

A generic type with type arguments provided, like Pair<int,string> above, is called a constructed type.

Any generic type where you specify the type parameters is considered a constructed type and constructed types are not allowed to be in pointers. That is why Nullable<int> is not allowed.

like image 69
Scott Chamberlain Avatar answered Sep 22 '22 14:09

Scott Chamberlain