Excel XLL returns different results

Hello everyone!
I am using Excel 2013, Visual Studio 2015. I began learning about Excel XLL. I wrote simple function which tests whether the argument is missing. Here's code:
1
2
3
4
5
6
7
8
9
#define DllExport __declspec(dllexport)

static LPWSTR rgFuncs[1][7] = { { L"GetSum", L"BU", L"GetSum" } };

DllExport double WINAPI GetSum(LPXLOPER12 arg)
{
    if (arg->xltype == xltypeMissing) return -1;
    return arg->xltype;
}

This code works as expected: if I miss argument, it gives me -1, and the type otherwise. But when I change code to this:
1
2
3
4
DllExport double WINAPI GetSum(LPXLOPER12 arg)
{
    return (arg->xltype == xltypeMissing) ? -1 : arg->xltype;
}

then, when I miss argument, it gives me 4294967295. Why?
Ternary expression returns one value of some predetermined type. It is important: it cannot return one type if condition is true and other otherwise. If types of expressions are different compiler makes an attempt to cast them to common type first.

If your operator expression types are: ()? int : unsigned; //←AFAIK
common type of int and unsigned is unsigned by C++ rules.
So your actual code is return (arg->xltype == xltypeMissing) ? (unsigned)-1 : arg->xltype;
Actual value of (unsigned)-1 is implementation defined, but common implementations make it UINT_MAX.
And maximum value of 4 bytes unsigned integer is 4294967295, which proves out hypothesis
http://en.cppreference.com/w/cpp/language/types#Range_of_values
@MiiNiPaa
Thanks a lot! Yes, I completely forgot that operands are converted into one of their types. The actual type of arg->xltype is DWORD (unsigned long). Basing on what you explained, the compiler converted "-1" into unsigned long, and I've got UINT_MAX. The change was simple: turn "-1" into "-1.". Now everything works perfectly. Thanks a lot!!
Topic archived. No new replies allowed.