小编San*_*lin的帖子

LuaJit FFI从C函数返回字符串到Lua?

说我有这个C函数:

__declspec(dllexport) const char* GetStr()
{
    static char buff[32]

    // Fill the buffer with some string here

    return buff;
}
Run Code Online (Sandbox Code Playgroud)

而这个简单的Lua模块:

local mymodule = {}

local ffi = require("ffi")

ffi.cdef[[
    const char* GetStr();
]]

function mymodule.get_str()
    return ffi.C.GetStr()
end

return mymodule
Run Code Online (Sandbox Code Playgroud)

如何从C函数中获取返回的字符串作为Lua字符串:

local mymodule = require "mymodule"

print(mymodule.get_str())
Run Code Online (Sandbox Code Playgroud)

c string lua ffi luajit

3
推荐指数
1
解决办法
1568
查看次数

标记为noexcept的函数内部是否有例外?

假设我有一个标记为的函数,noexcept但里面有一行代码可以抛出.那行代码将在try块中,异常将被捕获.这会导致什么吗?

void MyFunc() noexcept
{
    try {
        throw std::exception("...");
    } catch (const std::exception & e) {
        // I'll deal with it here...
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ exception noexcept c++11

3
推荐指数
2
解决办法
1176
查看次数

将无符号整数舍入为2的幂序列

我分配了大缓冲区,然后拆分成多个大小的块.这些尺寸从32开始,然后在每次增加时乘以2.

struct Node
{
    Node*           Next;
    void*           Data;
    const unsigned  Size;
};

Node* m_Buffers[16];
Run Code Online (Sandbox Code Playgroud)

这意味着缓冲区m_Buffers[0]的大小为32,缓冲区m_Buffers[1]的大小为64,依此类推.

并且一个函数接受一个数字并返回一个指定数字可以舍入到的大小的缓冲区.

void * GetBuffer(unsigned size)
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

例如,如果我请求一个384的缓冲区,那么我需要能够在512处将其舍入并从中返回缓冲区m_Buffers[4].

到目前为止,我正在使用循环来舍入:

void * GetBuffer(unsigned size)
{
    unsigned buffer_size = 32;

    while (buffer_size < size)
    {
        buffer_size *= 2;
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

但我很好奇是否有一种更好的方法可以不涉及循环.如果有一种方法可以将舍入数字转换为数组中的索引而不使用switch语句.

说实话,我甚至不确定标题是否正确.所以我为此道歉.

c c++ rounding

3
推荐指数
1
解决办法
119
查看次数

C++ std :: unique_ptr但不可移动?

我正在使用std :: unique_ptr在类上创建一些公共成员,这些成员必须是不可复制的或可移动的.但是std :: unique_ptr是可移动的,我想知道如果有人移动std :: unique_ptr包含的指针然后我尝试访问被移动的那个类的std :: unique_ptr成员会发生什么.

示例代码:

#include <cstdlib>
#include <memory>
#include <string>
#include <iostream>

struct obj
{
    obj(std::string id)
        : id(id)
    {

    }

    void identif()
    {
        std::cout << "i am " << id << std::endl;
    }

    std::string id;
};

struct objs
{
    // They must stay here no mater what.
    std::unique_ptr< obj > obj_a;
    std::unique_ptr< obj > obj_b;
    std::unique_ptr< obj > obj_c;

    objs()
        : obj_a(std::unique_ptr< obj >( new obj("object a") ))
            ,obj_b(std::unique_ptr< obj >( new obj("object …
Run Code Online (Sandbox Code Playgroud)

c++ unique-ptr c++11

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

C++ 11 - 在构造函数中移动基础数据类型?

我正在研究C++ 11中的移动语义,我很好奇如何在构造函数中移动基本类型,如boolean,integer float等.复合类型也像std :: string.

以下面的类为例:

class Test
{
public:
    // Default.
    Test()
        : m_Name("default"), m_Tested(true), m_Times(1), m_Grade('B')
    {
        // Starting up...
    }
    Test(const Test& other)
        : m_Name(other.m_Name), m_Times(other.m_Times)
        , m_Grade(other.m_Grade), m_Tested(other.m_Tested)
    {
        // Duplicating...
    }
    Test(Test&& other)
        : m_Name(std::move(other.m_Name)) // Is this correct?
    {
        // Moving...
        m_Tested = other.m_Tested; // I want to move not copy.
        m_Times = other.m_Times; // I want to move not copy.
        m_Grade = other.m_Grade; // I want to move not copy.
    }

    ~Test()
    { …
Run Code Online (Sandbox Code Playgroud)

move-semantics c++11

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

C++ std :: enable_if区分浮点数和有符号数?

如何区分有符号数和浮点数?

template<class T, typename std::enable_if<std::is_signed<T>::value>::type* = nullptr >
const char* GetTypeName(T t)
{
    if (std::numeric_limits<T>::max() == std::numeric_limits<signed char>::max())
        return "int8";
    else if (std::numeric_limits<T>::max() == std::numeric_limits<signed short>::max())
        return "int16";
    else if (std::numeric_limits<T>::max() == std::numeric_limits<signed int>::max())
        return "int32";
    else if (std::numeric_limits<T>::max() == std::numeric_limits<signed long long>::max())
        return "int64";
    else
        return nullptr;
}

template<class T, typename std::enable_if<std::is_unsigned<T>::value>::type* = nullptr >
const char* GetTypeName(T t)
{
    if (std::numeric_limits<T>::max() == std::numeric_limits<unsigned char>::max())
        return "uint8";
    else if (std::numeric_limits<T>::max() == std::numeric_limits<unsigned short>::max())
        return "uint16";
    else if (std::numeric_limits<T>::max() == …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae c++11

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

谷歌基准定制主

我希望在基准测试开始与Google的基准库一起运行之前调用自定义主函数.所以我可以设置好几件事.我搜索了很多,但我找不到任何东西.我应该手动修改宏吗?或者只是使用我的main函数并自己初始化基准测试.这会以任何方式影响库初始化吗?有没有其他方法可以不要求我修改该宏或复制它的内容?

benchmark\benchmark_api.h

// Helper macro to create a main routine in a test that runs the benchmarks
#define BENCHMARK_MAIN()                   \
  int main(int argc, char** argv) {        \
    ::benchmark::Initialize(&argc, argv);  \
    ::benchmark::RunSpecifiedBenchmarks(); \
  }
Run Code Online (Sandbox Code Playgroud)

c++ benchmarking

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

获取按位不为零的无符号整数的最大值

我正在尝试获取某个无符号整数类型的最大值,而不包含任何像<limits>. 所以我想我只是翻转无符号整数值 0 的位。

#include <iostream>
#include <limits>

int main()
{
    std::cout << (~0U) << '\n'; // #1
    std::cout << (std::numeric_limits< unsigned >::max()) << '\n'; // #2
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我对这些之间的细微差别不是很有经验。这就是为什么我要问使用第一种方法是否会发生某些意外行为或某些平台/架构问题。

c c++ bit-manipulation numeric-limits

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

为std :: function键入别名

我正在尝试为这种std::function类型添加别名,因为我计划将来使用其他东西.我希望能够轻松地做出改变.但是我从编译器收到一条我不太懂的错误信息.我理解这意味着什么,但我不明白这一点.

例:

#include <functional>

template < typename Ret, typename... Args > using MyFunc = std::function< Ret(Args...) >;

int main(int argc, char **argv)
{
    MyFunc<void(int)> fn;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

产生:

..\main.cpp|7|required from here|
..\main.cpp|3|error: function returning a function|
Run Code Online (Sandbox Code Playgroud)

c++ function c++11 type-alias

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

为什么即使在删除对象后我也可以访问成员函数?

我是C++的新手,从我到目前为止学到的东西,当你在一个指向堆上创建的东西的指针上调用delete时,该指针所指向的任何内容都会被擦除并释放内存,对吧?

但是当我在一个简单的类上尝试这个时:

class MyClass
{
    int _Id;
public:
    MyClass(int id) : _Id(id)
    {
        std::cout << "$Constructing the damn thing! " << _Id << std::endl;
    }
    ~MyClass()
    {
        std::cout << "?Destructing the damn thing! " << _Id << std::endl;
    }
    void Go_XXX_Your_Self()
    {
        std::cout << "%OooooooooO NOOOOOO! " << _Id << std::endl;
        delete this;
    }
    void Identify_Your_Self()
    {
        std::cout << "#Object number: " << _Id << " Located at: " << this << std::endl;
    }
};
Run Code Online (Sandbox Code Playgroud)

这些只是一些愚蠢的测试,看看删除如何工作:

int main()
{
    MyClass* …
Run Code Online (Sandbox Code Playgroud)

c++ function object member delete-operator

0
推荐指数
1
解决办法
1687
查看次数

C++ 11这行代码有什么作用?

我正在浏览Nano信号插槽源代码,并希望它可以帮助我使用C++ 11在我的应用程序中实现信号和插槽功能,我遇到了一些我以前没见过的代码.

部分代码:

/* ... */

template <typename Re_t> class function;
template <typename Re_t, typename... Args>
class function<Re_t(Args...)>
{
    void *m_this_ptr;
    Re_t(*m_stub_ptr)(void*, Args...);

/* ... */
Run Code Online (Sandbox Code Playgroud)

进一步来说:

class function<Re_t(Args...)>
Run Code Online (Sandbox Code Playgroud)

课程名称后做了什么?

c++ c++11

0
推荐指数
1
解决办法
267
查看次数

C++没有看到复制构造函数

我正在为我的项目开发一个小矢量类,基本上我不能从任何类型的数字构造一个矢量.即使它们都是不同的(例如Vector3(float,int,unsigned)).但是,我遇到了一个小问题,我迷失在这些模板及其顺序中.现在我的算术运算符无法正常工作.

这是vector类的代码:

#include "Defines.hpp" // Typedefs for the fundamental types...
#include "Math/Base.hpp" // Various numerical functions...

template<class T, class Enable = void>
class Vector3;

template<class T>
class Vector3<T, typename std::enable_if<std::is_arithmetic<T>::value >::type>
{
public:
    Vector3()
        : X(0), Y(0), Z(0)
    {

    }

//    explicit Vector3(T s)
//        : X(s), Y(s), Z(s)
//    {
//
//    }

    template <class U, typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
    explicit Vector3(U s)
        : X(getBounded<T>(s)), Y(getBounded<T>(s)), Z(getBounded<T>(s))
    {

    }

//    explicit Vector3(T x, T y, T z)
//        : …
Run Code Online (Sandbox Code Playgroud)

c++ templates constructor c++11

-4
推荐指数
1
解决办法
130
查看次数