2016-09-09 2 views
5

GCC kann einige Ausdrücke nicht als Konstante auswerten. Clang ist aber nett damit.Constexpr: Vergleich zu nullptr - Bug oder Feature?

/* 
*/ 
constexpr int foo(const int * array) 
{ 
    if (array == nullptr) // Error: '(((const int*)(& array)) == 0u)' is not a constant expression 
    { 
    return 0; 
    } 

    return 1; 
} 

constexpr int bar() 
{ 
    int array[100] = {}; 

    return foo(array); 
} 

static_assert(bar() == 1, "outch..."); // Does not compile. See above. 
static_assert(foo(nullptr) == 0, "okay"); 

constexpr int i[100] = {}; 
static_assert(foo(i) == 1, "okay"); 

funktioniert auch nicht:

constexpr int foobar() 
{ 
    int array[100] = {}; 
    int *ar = array; 
    if (ar == nullptr) // Error... 
    { 
    return 0; 
    } 
    return 1; 
} 

static_assert(foobar() == 1, "okay"); 

Gleiche:

constexpr int foo2() 
{ 
    int *a = nullptr; 
    if (a == nullptr) // Error... 
    { 
    return 0; 
    } 
    return 1; 
} 

static_assert(foo2() == 0, "okay"); 

Live Example

Ich meine, ein Vergleich zu nullptr etwas anderes als ein Vergleich zu einer sein sollte, andere zufällige Adresse.

Würden Sie sagen: Es ist ein Fehler oder eine Frage der Interpretation? Für mich ist es schwierig, den gleichen Code für beide Compiler zu schreiben ...

Dieser Fehler tritt für GCC 5.0 bis 5.4 auf. In GCC 6+ kompiliert nur foobar() nicht.

Antwort

Verwandte Themen