lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Wed, Apr 04, 2007 at 06:39:43PM +0200, Wim Couwenberg wrote:
> NULL is a macro that expands to 0 in C++, which is of type int.  The
> implicit conversion to pointer is only allowed for a constant
> expression, which this comma expression is not.

The literal 0 (and NULL, which is defined to be 0) is explicitly comparable and
assignable to a pointer, even in C++ (, so I don't think its correct
to say 0 is of type int.

Unlike 0, the result of a comma expression has a type, int in this case, and
int cannot be assigned to a pointer in C++, or without warnings in C
(, not sure if its a bug.  Of course, is plainly not legal.

const char *func()
       return 0;

const char *func()
       return 0, 0;

int zero(){return 0;}
const char *func()
       return zero();

I've seen similar surprises with the ternary operator. The ternary expression
can sometimes have a type chosen for its resulting expression that surprises

	% cat
	extern void x(char*);
	void test(int i) {
			char* p = "hi";
			x(i ? "hi" : "hi");
			x(i ? p : "hi");
	% g++ -Wall -c In function ‘void test(int)’: error: invalid conversion from ‘const char*’ to ‘char*’ error:   initializing argument 1 of ‘void x(char*)’

Literal strings being promoted to const char*, I can accept that, but
why would (i ? "hi" : "hi") be char*, but (i ? p : "hi") suddenly becomes
const char*, when the only thing new is a non-const ptr p?

Btw, I've never really understood the lua idiom of doing a "return
luaL_error();", it just obfuscates the fact that the function is not returning,
IMO.  Is it a hold-over from an ancient lua that didn't use longjmp()?

In this case, since the "official" return type of luaL_error() (which is a lie,
since it doesn't return) doesn't match the return type needed, trying to use
the comma operator to get around the warning seems even more obfuscated, it
took me a second to understand why you would even be trying to write that code,
since the second branch of the comma is obviously never going to be reached!