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++ (test1.cc), 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
(test2.cc), not sure if its a bug.  Of course, test3.cc is plainly not legal.

::::::::::::::
test1.cc
::::::::::::::
const char *func()
{
       return 0;
}

::::::::::::::
test2.cc
::::::::::::::
const char *func()
{
       return 0, 0;
}

::::::::::::::
test3.cc
::::::::::::::
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
me:

	% cat test5.cc
	extern void x(char*);
	void test(int i) {
			char* p = "hi";
			x("hi");
			x(i ? "hi" : "hi");
			x(i ? p : "hi");
	}
	% g++ -Wall -c test5.cc
	test5.cc: In function ‘void test(int)’:
	test5.cc:6: error: invalid conversion from ‘const char*’ to ‘char*’
	test5.cc:6: 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!

Cheers,
Sam