typeof, __typeof__ (C23)

Neu im C23-Standard ist der typeof Operator ein unärer Operator, der den Typ eines Ausdrucks zurückgibt. Sie kann in Typdeklarationen, Typ casts, Typüberprüfungen usw. verwendet werden. Er ruft den Typ einer Variablen, Funktion oder eines beliebigen C-Ausdrucks ab.

Die __typeof__ Schlüsselwort (keyword) ist eine microsoftspezifische Erweiterung, die die gleiche Funktionalität bietet wie typeof. Die __typeof__ Schlüsselwort (keyword) unterscheidet sich nur davontypeof, dass sie bei der Kompilierung für alle C-Versionen (nicht nur/std:clatest) verfügbar ist, und es kann das Portieren von Code zwischen anderen Compilern vereinfachen, die unterstützt __typeof__werden.

Syntax von typeof

typeof(type)
typeof(constant-expression)
__typeof__(constant-expression)

typeof-Beispiel

In diesem Beispiel wird typeof()verwendet, das Verhalten ist jedoch identisch, wenn Sie es verwenden __typeof__.

// Compile with /std:clatest

#include <stdio.h>

double func()
{
    3.14;
}

#define POINTER(T) typeof(T*)

int main()
{
    auto a = func(); // the type for a (double) is inferred, but requires initialization at point of declaration
    typeof(func()) b; // the type for b is double, but didn't have to be initialized at point of declaration

    // Some declarations using typeof
    POINTER(int) p1 = NULL; // p1 is int*

    typeof(double(void))* pFunc = func; // pFunc is a pointer to a function that takes no arguments and returns a double
    printf("pFunc() returns %f\n", pFunc());

    return 0;
}

Anforderungen

Erfordert Visual Studio 17.9 oder höher oder cl.exe Version 19.39.33428 oder höher. typeofKompilieren Mit /std:clatest.

Weitere Informationen

/std (Standardversion für die Sprache festlegen)