My "problem" is that I have some large chunk of code where I have happily used NULL as ((void*)0) and it compiles and runs correctly, without the error message error: invalid initialization of reference for argument. I have not been able to recreate that situation in the code below, though.
USE_S defines if we actually use an object (=1). And if we don't (=0) then we can get the error with WORKS (=0) or OK with WORKS (=1).
Code: Select all
// #include <xs1.h> // Not needed
#include <stdlib.h> // Defines NULL not correct to use here
void fr (int &?y) { // XMOS Programming Guide, XM004440A p47
if (!isnull(y)) {
// We know y is not null so can use it here
}
}
void fa (int (&?a)[5]) { // XMOS Programming Guide, XM004440A p47
if (!isnull(a)) {
// We know a is not null so can use it here
}
}
typedef struct s_t {
int val;
} s_t;
#define USE_S 0
#define WORKS 1 // 0 or 1 valid if USE_S is 0
void fs (s_t &?s) {
#if (USE_S==1)
// We know s is not null so can use it here
#else
// s is always valid
#endif
}
int main() {
int y;
fr (y);
fr (null);
int a[5];
fa (a);
fa (null);
#if (USE_S==1)
s_t s; // Only defined when needed
#define MY_S s
#else
#undef NULL
#if (WORKS==1)
#define MY_S null
#else
#define NULL ((void*)0) // Same as in "stdlib.h". Same problem:
#define MY_S NULL // "error: invalid initialization of reference for argument"
#endif
#endif
fs (MY_S); // s(ok), null(ok) or NULL(err)
return 0;
}