小编Gui*_*cot的帖子

完美转发仿函数

我想知道使用完美转发仿函数的正确方法是什么?这是两个代码段.哪一个是最好的,如果不是,哪个是最好的形式?

template<typename T, typename... Args>
void callMe(T&& func, Args&&... args) {
    func(std::forward<Args>(args)...);
}
Run Code Online (Sandbox Code Playgroud)

要么

template<typename T, typename... Args>
void callMe(T&& func, Args&&... args) {
    std::forward<T>(func)(std::forward<Args>(args)...);
}
Run Code Online (Sandbox Code Playgroud)

编辑:

它会影响重载分辨率吗?如果funcoperator()具有REF-预选赛&&或者const &,我应该做的后一个版本,我应该关心哪些超载我打电话?

谢谢!

c++ templates functor perfect-forwarding

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

std :: string参数的右值

当我传递文本时,以下代码中的LVALUE和RVALUE在实践上有什么区别?我的意思是,在这种特定的字符串情况下(其中字符串是字符串文字),使用RVALUE(&&)有什么好处吗?

void write_Lvalue(const std::string &text) {
    //...
}

void write_Rvalue(const std::string &&text) {
    //...
}

int main() {
    write_Lvalue("writing the Lvalue");
    write_Rvalue("writing the Rvalue");
}
Run Code Online (Sandbox Code Playgroud)

c++ rvalue-reference temporary-objects c++11

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

到处使用共享指针有哪些陷阱?

我们的代码库使用许多向量。其中一些向量彼此共享对象,即它们包含std::shared_ptr共享对象。问题是:代码库仍处于活跃的开发阶段,通常需要将一些包含值的向量重写为包含共享指针的向量。这很乏味。所以我想出了一个想法:简单地将所有向量作为共享指针的向量。

问题是:可以吗?我应该注意哪些注意事项?会出现什么问题?

我做了一些简单且可能幼稚的测量,在初始化和查询值向量和共享指针向量时,它们之间的性能实际上没有区别。

如果性能不是问题,还有什么我应该注意的吗?

c++ pointers vector

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

T ::*在函数参数列表的声明中表示什么?

我在我的代码中声明了一个特定的键盘回调函数:

void keyboardEventCallback(const pcl::visualization::KeyboardEvent &event, void* viewer_void, void* widget_void);
Run Code Online (Sandbox Code Playgroud)

键盘事件是传递给回调函数的实际事件,viewer_void参数是指向PCLVisualizer类的指针,该类生成用于呈现的窗口,而widget_void是指向与Qt接口的窗口小部件的指针.

在pcl的文档中,注册函数传递用于注册键盘函数的参数

boost::signals2::connection registerKeyboardCallback(void(T::*callback)(const pcl::visualization::KeyboardEvent&, void*), T& instance, void* cookie=nullptr)
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,注册函数声明中的含义T::*什么,为什么我不允许传递这个:

m_vis->registerKeyboardCallback(keyboardEventCallback, (void*)&m_vis, (void*)this);
Run Code Online (Sandbox Code Playgroud)

m_vis可视化器在哪里,keyboardcallback是回调,这是小部件.

为什么我不能这样注册.这是针对点云库的.

c++ qt callback void point-cloud-library

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

C++ - 将rapidjson::Document 作为参数传递给函数

我将指针传递给rapidjson::Document作为参数。

foo(rapidjson::Document* jsonDocument)
{
    std::cout << jsonDocument["name"] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

但我无法jsonDocument["name"]访问 name 属性。

尝试不使用指针会导致错误:

error: 'rapidjson::GenericDocument<Encoding, Allocator, StackAllocator>::GenericDocument(const rapidjson::GenericDocument<Encoding, Allocator, StackAllocator>&) [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; StackAllocator = rapidjson::CrtAllocator]' is private
GenericDocument(const GenericDocument&);
Run Code Online (Sandbox Code Playgroud)

有人能帮我吗?

c++ json arguments rapidjson

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

C++构造函数SFINAE

#include <iostream>

using namespace std;

template <typename T>
class test {
public:
    T value;

    template <typename... Args, typename = decltype(T())>
    test(Args... args): value(args...)
    {
       cout <<"ctor running\n";
    }

    template <typename... Args>
    test(Args...) : value(1)
    {
       cout <<"ctor unspec  running\n";
    }
};


class t
{
public:
    t() = delete;
    explicit t(int) {}
};


int main()
{
    test<t> h;
}
Run Code Online (Sandbox Code Playgroud)

我试图为constructor创建的对象(h)调用第二个.我不知道为什么会收到此错误:

prog.cc: In function 'int main()':
prog.cc:45:13: error: call of overloaded 'test()' is ambiguous
     test<t> h;
             ^
prog.cc:25:5: …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae c++11 c++14

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

仅允许通过NGINX中的IP白名单访问某些位置

我正在使用Sucuri扫描仪通知我登录尝试失败,我目前每天收到大约50封以上的电子邮件.我已经尝试了几种不同的方法来阻止访问wp-login.phpwp-admin而没有任何运气,因为我认为这些规则可能不适用于子域(或者通常只是吮吸).

server {
    # Primary domain, secondary domain and subdomains are explicitly 
    # declared so that I can generate certs using CertBot
    server_name primarydomain.com
                secondarydomain.com
                subdomain1.primarydomain.com
                subdomain2.primarydomain.com
                subdomain3.primarydomain.com;

    client_max_body_size 20M;
    root /home/username/www/primarydomain.com/public_html;
    index index.php;
    error_log /home/username/www/primarydomain.com/logs/error.log error;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }

    # This doesn't seem to block access
    location /wp-login.php {
        allow XXX.XXX.XXX.XXX;    # this is my …
Run Code Online (Sandbox Code Playgroud)

subdomain wordpress nginx multisite

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

在命名空间中使用自己的类比较std :: vector不会编译

由于找不到比较运算符,因此以下代码无法编译。

#include <vector>
#include <iostream>
#include <string>

namespace Cool {
    struct Person {
        std::string name;
    };
}

bool operator==(const Cool::Person&  p1, const Cool::Person& p2) {
    return p1.name == p2.name;
}

int main(int, char *[])
{
    std::vector<Cool::Person> a{ {"test"} };
    std::vector<Cool::Person> b{ {"test"} };
    bool ok = a == b;
    std::cout << ok << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

经过一些实验,我发现以下代码可以完美编译:

#include <vector>
#include <iostream>
#include <string>

namespace Cool {
    struct Person {
        std::string name;
    };

    bool operator==(const Person&  p1, const Person& p2) {
        return …
Run Code Online (Sandbox Code Playgroud)

c++ stl namespaces operator-overloading stdvector

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

&amp;&amp; 对 lambda 表达式有什么好处?

我看到的代码如下:

template<class Function>
void MyFunc(Function&& function) { function(...); }
Run Code Online (Sandbox Code Playgroud)

&&与仅按值复制函数相比,这里的优势是什么?作为参考,我使用的是 C++14。

c++ lambda templates perfect-forwarding c++14

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

来自方法的 C++ 引用变量

这样做有意义吗:

class SomeClass
{
public:
    static Object getObject()
    {
        return Object("example")
    }
};

int main()
{
    const Object& myObject = SomeClass::getObject();

    // do something with myObject
}
Run Code Online (Sandbox Code Playgroud)

或者这与只是获取对象一样

const Object myObject = SomeClass::getObject();
Run Code Online (Sandbox Code Playgroud)

?

c++ reference

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