小编Ale*_* V.的帖子

这个函数返回什么

function f(...)
  return ...
end
Run Code Online (Sandbox Code Playgroud)

我称之为:

f()
Run Code Online (Sandbox Code Playgroud)

a = f()
print(a) -- echoes 'nil', same as print(nil)
Run Code Online (Sandbox Code Playgroud)

print(f()) -- echoes newline, same as print(), that is, no args
t = {f()} -- same as t = {}
Run Code Online (Sandbox Code Playgroud)

那么,f()返回什么?

更新:不知道函数可以返回'void',同时发现这个http://lua-users.org/lists/lua-l/2011-09/msg00289.html.

lua variadic-functions

2
推荐指数
1
解决办法
1148
查看次数

wstring :: c_str()包含垃圾

我有一个std::wstring decode(const char *s)功能.

我这样使用它:

const char *src = "some string";
const wchar_t *result = decode(src).c_str();
Run Code Online (Sandbox Code Playgroud)

我总是得到垃圾result[0],有时result[1]也是垃圾.

当我以另一种方式使用它时,我不会得到垃圾:

std::wstring res = decode(src);
const wchar_t *result = res.c_str();
Run Code Online (Sandbox Code Playgroud)

我的decode功能定义如下,它确实起作用了.唯一的问题是调用代码(上图).

std::wstring decode(const char *s, UINT cp=CP_ACP)
{
    if(s == NULL)
        return std::wstring();
    int length = ::MultiByteToWideChar(cp, 0, s, -1, NULL, 0 );
    wchar_t *buf = new wchar_t[length];
    ::MultiByteToWideChar(cp, 0, s, -1, buf, length);
    std::wstring r(buf);
    delete[] buf;
    return r;
}
Run Code Online (Sandbox Code Playgroud)

我使用Visual C++ 2008 SP1进行编译.

c++ visual-studio-2008 temporary-objects

1
推荐指数
2
解决办法
1418
查看次数