小编dec*_*iar的帖子

虚函数和性能C++

在你对重复的标题感到畏缩之前,另一个问题不适合我在这里提出的要求(IMO).所以.

我真的想在我的应用程序中使用虚函数使事情变得容易一百倍(不是OOP的全部内容;)).但我读到了他们以性能成本出现的某个地方,只看到了过早优化的同样过时的炒作,我决定在一个小的基准测试中快速调整它:

CProfiler.cpp

#include "CProfiler.h"

CProfiler::CProfiler(void (*func)(void), unsigned int iterations) {
    gettimeofday(&a, 0);
    for (;iterations > 0; iterations --) {
        func();
    }
    gettimeofday(&b, 0);
    result = (b.tv_sec * (unsigned int)1e6 + b.tv_usec) - (a.tv_sec * (unsigned int)1e6 + a.tv_usec);
};
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include "CProfiler.h"

#include <iostream>

class CC {
  protected:
    int width, height, area;
  };

class VCC {
  protected:
    int width, height, area;
  public:
    virtual void set_area () {}
  };

class CS: public CC {
  public:
    void set_area () { area …
Run Code Online (Sandbox Code Playgroud)

c++ performance virtual inheritance

7
推荐指数
2
解决办法
1224
查看次数

Lua合作例程

我试图了解如何使用协同例程来"暂停"一个脚本并等待一些处理完成后再恢复.

也许我正在以错误的方式看待惯例.但我的尝试结构类似于这个答案中给出的例子.

循环loop.lua永远不会达到第二次迭代,因此永远不会达到i == 4退出C代码中的运行循环所需的条件.如果我不屈服loop.lua,则此代码按预期执行.

main.cpp中

#include <lua/lua.hpp>

bool running = true;

int lua_finish(lua_State *) {
    running = false;
    printf("lua_finish called\n");
    return 0;
}
int lua_sleep(lua_State *L) {
    printf("lua_sleep called\n");
    return lua_yield(L,0);
}

int main() {
    lua_State* L = lua_open();
    luaL_openlibs(L);

    lua_register(L, "sleep", lua_sleep);
    lua_register(L, "finish", lua_finish);

    luaL_dofile(L, "scripts/init.lua");

    lua_State* cL = lua_newthread(L);
    luaL_dofile(cL, "scripts/loop.lua");

    while (running) {
        int status;
        status = lua_resume(cL,0);
        if (status == LUA_YIELD) {
            printf("loop yielding\n");
        } else …
Run Code Online (Sandbox Code Playgroud)

c++ scripting lua coroutine

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

Node.js不保留请求标头的大写

我将节点js用作反向代理,主要使用http和http-proxy模块。在将请求发送到nodejs以重定向到我的应用程序之一时,我必须传递全部都是大写的请求标头。但是,nodejs或http会将所有大写字母转换为小写字母,因此我的应用程序验证之一失败。

我的代码段是:

http.createServer(function (request, response) {

    var redirection = 'http://localhost:8000';
    var path = url.parse(request.url).path;

    switch (path) {
        case '/health':
            proxy.web(request, response, { target: redirection });
            break;
}).listen(8080);
Run Code Online (Sandbox Code Playgroud)

传递的请求标头是:

curl -H "X-AUTH: PBxqcEm5sU743Cpk" -X GET http://localhost:8080/health
Run Code Online (Sandbox Code Playgroud)

现在发生的是,标题“ X-AUTH”已转换为“ x-auth”,而我的应用程序无法对其进行验证。在我的应用程序中,标头匹配区分大小写。

从节点js请求对象打印的请求标头是:

{ host: 'localhost:8080',
  'user-agent': 'curl/7.47.1',
  accept: '*/*',
 'x-auth': 'PBxqcEm5sU743Cpk' }
Run Code Online (Sandbox Code Playgroud)

我的要求是保留请求中传递的标头的大写字母,以便我的应用程序可以对其进行验证和授权。请让我知道是否有任何方法可以实现这一目标

非常感谢

node.js

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

boost :: ptr_container和std :: vector <shared_ptr>

看完后timdays回答这个问题,我很好奇的区别boost::ptr_containerstd::vector<shared_ptr>.我的印象是,下一个boost::ptr_container在给它的指针有所有权,并在释放将召开它包含指针的析构函数,无论其居民其他参考资料.这与a的目的相反,std::vector<shared_ptr>如果ref计数为0,那么在释放之后它只会释放指针本身?

如果是这种情况(我假设不是这样),那么为什么甚至Boost文档示例都会比较两者,就好像它们的目的相似,为什么timday的答案会提出一个boost::ptr_container与a的目的非常不同的答案std::vector<shared_ptr>.

c++ boost memory-management smart-pointers stdvector

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

D中的模糊结构构造函数

我在理解如何处理D中构造函数的模糊性方面遇到了一些麻烦.

struct mydta {
    int a = 2;
    int b = 3;

    this(int c) {
        a = c / 2;
        b = c * 2;
    }
    this(float c) {
        a = cast(int) c / 2;
        b = cast(int) c * 2;
    }

    static mydta afvec = mydta(4.3);
    static mydta aivec = mydta(5);
}
Run Code Online (Sandbox Code Playgroud)
  • afvec 数据值为2和8.
  • aivec 具有数据值5和3.

这意味着从语法中按预期afvec调用this(float c).

但是aivec做了类似的任务aivec.a = 5.

我在此推断,发现以下内容是合法的:分别aivec = mydta(5, 4);给出aivec值5和4.

任何想法如何绕过这个隐式初始化,以便我可以访问我的构造函数:this(int …

constructor struct d initialization ambiguity

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

D动态阵列 - RAII

我承认在这一点上我对D没有深刻的理解,我的知识纯粹依赖于我所阅读的文档和我尝试过的几个例子.

在C++中,您可以依赖RAII习惯用法在退出本地范围时调用对象的析构函数.

D你能来吗?

我理解D是一种垃圾收集语言,它也支持RAII.为什么以下代码不会清除内存,因为它离开了一个范围呢?

import std.stdio;

void main() {
      {
            const int len = 1000 * 1000 * 256; // ~1GiB

            int[] arr;
            arr.length = len;
            arr[] = 99;
      }

      while (true) {}
}
Run Code Online (Sandbox Code Playgroud)

无限循环用于保持程序打开以使残留内存分配容易可见.

下面显示了C++中等效的相同程序的比较. C++ v D.

可以看出,C++在分配后立即清理了内存(刷新率使得它看起来好像分配了更少的内存),而D保留了它,即使它已经离开了范围.

因此,GC什么时候清理?

garbage-collection memory-management d raii

5
推荐指数
2
解决办法
625
查看次数

不能在 Node.js 中使用 IP 作为自签名证书

我正在尝试在 Node.js 中使用 HTTP 自签名证书。

我已经使用我编写的以下脚本生成了证书:

生成文件

#!/bin/bash
read -p "FQDN: " FQDN

# for easy testing
rm ROOT.*
rm SERVER.*

openssl genrsa 4096 > ROOT.key
openssl req -x509 -nodes -sha256 -new -key ROOT.key -days 365 -subj "/C=AU/CN=example" > ROOT.crt

openssl req -newkey rsa:4096 -nodes -sha256 -keyout SERVER.key -subj "/C=AU/CN=${FQDN}" > SERVER.csr
openssl x509 -days 365 -req -in SERVER.csr -CA ROOT.crt -CAkey ROOT.key -CAcreateserial > SERVER.crt
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用以下方法测试证书:

测试.js

let fs = require('fs')
let https = require('https')

// HTTP server
https.createServer({ …
Run Code Online (Sandbox Code Playgroud)

javascript https openssl self-signed node.js

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

错误:对重载模板使用删除的功能

我正在尝试模板专门化,但是无法确定以下原因char const * const(尽管是有效类型)无法解析的原因。

template <typename A, typename B> B foo (A) = delete;

template <> void foo (char *) {}
template <> void foo (char const * const) {}

int main () {
    { // types OK
        char const * const a = nullptr;
        char * b = nullptr;
    }

    char * data;
    foo<char *, void>(data); // OK
    foo<char const * const, void>(data); // ERROR
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误

error: use of deleted function ‘B …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-specialization

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

什么是C++中的**

我目前正在阅读一些C++源代码,我遇到了这个:

 double **out;
 // ... lots of code here 

 // allocate memory for out
 out = new double*[num];
Run Code Online (Sandbox Code Playgroud)

不完全确定它的作用或含义.它是指针......指向另一个指针吗?

还有以下内容:

 double ***weight;

 // allocate memory for weight
 weight = new double**[numl];
Run Code Online (Sandbox Code Playgroud)

我很困惑:P,任何帮助都表示赞赏.

c c++ memory pointers

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

隐藏'静态'类变量

所以我最近发现了一些使用特定技术的源代码(成语?)我以前没见过; 简而言之; 它不是使用相关类的静态变量,而是在类源文件中使用局部变量.

myclass.h

class myclass {
    //static int myint;

public:
    myclass();
    ~myclass();
    int count();
};
Run Code Online (Sandbox Code Playgroud)

myclass.cpp

#include "myclass.h"

int myint = 0;

myclass::myclass() {
    myint++;
}
myclass::~myclass() {
    myint--;
}

int myclass::count() {
    return myint;
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include "myclass.h"
#include <iostream>

int main() {
    myclass aclass;
    myclass theclass;

    std::cout << theclass.count(); //outputs 2
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么有人会采用这种方法而不是使用静态变量?

我对它的看法是,理想情况下,变量只能为myclass类(私有静态)所知,并且继承根本不重要(在这种情况下),这可能会阻止其他人知道这个变量.但这是我能看到的唯一优势; 不确定这是否值得保证.

同样的问题适用于私有的(静态/非静态)成员函数; 当继承不重要时.

编辑:读完之后,我要做的是因为有些人仍然使用C编程风格...

c++ static-members static-functions

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