2016-04-06 14 views
3

Ich habe ein Makro wie folgt aus:Erwartete Ausdruck vor 'typeof' OR erwartet Primärausdruck vor 'typeof'

#include <stdio.h> 
#include <stddef.h> 

#define m_test_type(e)        \ 
    do {           \ 
     if (typeof(e) == typeof(char [])) {  \ 
      printf("type is char []\n");   \ 
     } else          \ 
     if (typeof(e) == typeof(int)) {   \ 
      printf("type is int\n");    \ 
     } else {         \ 
      printf("type is unknown\n");   \ 
     }           \ 
    } while (0) 

int main() { 
    char s[] = "hello"; 

    m_test_type(s); 

    return 0; 
} 

Beim Übersetzen mit gcc-I-Fehler erhalten folgende:

prog.cpp: In function 'int main()': 
prog.cpp:6:14: error: expected primary-expression before 'typeof' 
      if (typeof(e) == typeof(char *)) {   \ 
      ^
prog.cpp:19:2: note: in expansion of macro 'm_test_type' 
    m_test_type(s); 
^
prog.cpp:6:14: error: expected ')' before 'typeof' 
      if (typeof(e) == typeof(char *)) {   \ 
      ^
prog.cpp:19:2: note: in expansion of macro 'm_test_type' 
    m_test_type(s); 
^
prog.cpp:9:14: error: expected primary-expression before 'typeof' 
      if (typeof(e) == typeof(int)) {   \ 
      ^
prog.cpp:19:2: note: in expansion of macro 'm_test_type' 
    m_test_type(s); 
^
prog.cpp:9:14: error: expected ')' before 'typeof' 
      if (typeof(e) == typeof(int)) {   \ 
      ^
prog.cpp:19:2: note: in expansion of macro 'm_test_type' 
    m_test_type(s); 
^
+0

'typeof' ist nicht Standard-C, es ist ein [GNU-Erweiterung] (https://gcc.gnu.org/onlinedocs/gcc/Typeof .html) in GCC. Welchen Compiler benutzen Sie? – unwind

+0

Ich benutze GCC-Compiler –

+0

Code muss ein SWAG nehmen, was "e" ist. Es hat keine Möglichkeit zu wissen und auch nicht "typeof". –

Antwort

3

Dies ist nicht Standard-C, so wird es nur mit GCC-Setup kompilieren, um ein nicht-Standard-Compiler zu sein. Das bedeutet, dass Sie wahrscheinlich keine Optionen wie -std=c11 oder -pedantic verwenden sollten, um diese Kompilierung durchzuführen. Ich würde das aber nicht empfehlen.

Ich würde stattdessen empfehlen alle Nicht-Standard-GCC Mist loszuwerden und rein, anstatt Standard-C schreiben:

#include <stdio.h> 

#define m_test_type(e)      \ 
    printf(_Generic((e),      \ 
      char*: "type is char*\n",  \ 
      int:  "type is int\n",  \ 
      default: "type is unknown\n" \ 
     )); 

int main() { 
    char s[] = "hello"; 

    m_test_type(s); 

    return 0; 
} 
6

Ich glaube nicht, Sie typeof verwenden können für Typen Gleichheit zu testen. Wenn man sich die gcc manual aussehen wird typeof (expr) statisch durch die Art des Ausdrucks ersetzt, die Sie Variablen deklarieren können:

int i; 
typeof(&i) p; 

In diesem Fall wird die letzte Anweisung gleichwertig sein int* p;

Allerdings, wenn Sie verwenden typeof in einer if-Anweisung wie das Ihr ein Fehler wäre, da es äquivalent wäre so etwas wie

if (char* == char *) 

zu schreiben, die den Fehler verursacht.

2

i builtin functions mithilfe dieser Code jetzt ausführen können:

#include <stdio.h> 

#define m_test_type(e)             \ 
    do {                 \ 
     if (__builtin_types_compatible_p(typeof(e), typeof(char []))) { \ 
      printf("type is char []\n");         \ 
     } else               \ 
     if (__builtin_types_compatible_p(typeof(e), typeof(int))) {  \ 
      printf("type is int\n");          \ 
     } else {              \ 
      printf("type is unknown\n");         \ 
     }                \ 
    } while (0) 

int main() { 
    const char s[] = "hello"; 

    m_test_type(s); 

    return 0; 
} 
+2

Aber Sie drucken '" Typ ist char * \ n "', wenn der Typ tatsächlich 'char []' ist. Zum Beispiel 'm_test_type (char *);' und 'm_test_type (&s[0]);' beide Druck ' "Typ ist unbekannt \ n"' Wie 'sizeof (x)', 'typeof (x)' ist einer dieser Orte, wo ein. Array 'x' wird nicht zu einem Zeiger' & (x) [0] '. –

+0

danke für den Kommentar, es war Just ein Copy/Paste-Fehler, ich habe es jetzt behoben –