小编Yak*_*ont的帖子

C++ lambda.如何捕获指针?

例如,有一种方法:

void foo( A* p )
{
    auto l = [=](){ /* Do Something using p */ };

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

我应该如何捕获指针:通过引用或值?里面的lambda p没有改变,只是使用了它的方法.

c++ lambda pointers object-lifetime c++11

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

C++11 thread::id 表示无线程的特殊值

该手册页指出

此类的实例还可能包含不代表任何线程的特殊不同值。

但是我找不到这个特殊值是什么或者它在哪里定义(在<线程>中查看)。有任何想法吗?

c++ multithreading c++11 stdthread

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

为什么C++ STL容器使用"小于"运算符<而不是"等于"运算符==作为比较器?

在自定义类中实现比较器运算符时std::map,我遇到了这个问题,无法看到任何问题.

除了上述问题,还有兴趣简要了解一下,如何operator<工作std::map.

问题的起源:

struct Address {
  long m_IPv4Address;
  bool isTCP;
  bool operator< (const Address&) const;  // trouble
};
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading map comparator

4
推荐指数
2
解决办法
5966
查看次数

为什么模板类有未使用的类型?

我正在审查boost单元库,我很困惑为什么boost :: units :: unit类有一个额外的模板参数.这是一个例子:

http://www.boost.org/doc/libs/1_57_0/boost/units/unit.hpp

template<class Dim,class System, class Enable>
class unit
{
    public:
        typedef unit<Dim, System>   unit_type;
        typedef unit<Dim,System>    this_type;
        typedef Dim                 dimension_type; 
        typedef System              system_type;

        unit() { }
        unit(const this_type&) { }
        //~unit() { }  

        this_type& operator=(const this_type&) { return *this; }

        // sun will ignore errors resulting from templates
        // instantiated in the return type of a function.
        // Make sure that we get an error anyway by putting.
        // the check in the destructor.
        #ifdef …
Run Code Online (Sandbox Code Playgroud)

c++ templates generic-programming boost-units template-classes

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

使用std :: function参数重载函数:为什么const方法永远不会被调用?

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

using namespace std;

class A
{
    public:
    void doStuff(function<void (const string *)> func) const
    {
        cout << "Const method called" << endl;
        for(const auto& i_string : m_vec)
            func(i_string);
    }

    void doStuff(function<void (string *)> func)
    {
        cout << "Non-const method called" << endl;
        doStuff([&func](const string *str)
        {
            auto mutableString = const_cast<string *>(str);
            func(mutableString);
        });
    }

private:
    vector<string *> m_vec;
};

int main()
{
    auto a = A{};

    a.doStuff([](string *str){
        *str = "I modified …
Run Code Online (Sandbox Code Playgroud)

c++ overloading const c++11

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

如何将参数添加到va_list

我有以下代码:

int __dmasprintf (char **s, const char *format, ...) {

    char buf[512];
    va_list arg;
    int ret;
    va_start(arg,format);
    ret = vsprintf(buf, format, arg);
    va_end(arg);

    *s = strdup(buf);
    if (*s == NULL) return -1;
    return 0;   
}
Run Code Online (Sandbox Code Playgroud)

我想在va_list arg之前的调用中添加一个参数,vsprintf()因为我format在最后包含了1个额外的参数.

如何添加一个参数(例如char * myarg)va_list arg?

或者是否可以通过vsprintf()自定义列表?

c variadic-functions

4
推荐指数
2
解决办法
2834
查看次数

mersenne twister线程对cpp是否安全

#include <random>

int f() {

    std::random_device seeder;
    std::mt19937 engine(seeder());
    std::uniform_int_distribution<int> dist(1, 6);

    return dist(engine);

}
Run Code Online (Sandbox Code Playgroud)

多个线程可以安全地调用此函数吗?功能线程安全吗?这是reduntant调用std::random_device seeder;和std::mt19937 engine(seeder());每一次?

c++ multithreading mersenne-twister c++11

4
推荐指数
2
解决办法
2077
查看次数

虚拟函数和虚函数表如何工作?

虽然 C++ 标准将虚拟调度的实现留给了编译器,但目前只有 3 个主要编译器(gcc、clang 和 msvc)。

当您通过指向抽象基类的指针调用抽象基类上的方法时,它们如何实现虚拟调度?构建期间如何设置 vtable?

一个简单的“好像”示例会很有用。

c++ polymorphism language-implementation vtable dynamic-dispatch

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

如何推断变量参数后的模板参数?

我有以下模板功能:

template <typename...Args, typename Func>
void call(const char *name, Args...args, Func f)
{
        f(3);
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用它时,就像

    call("test", 1, 2, 3, [=](int i) { std::cout<< i; });
Run Code Online (Sandbox Code Playgroud)

编译器抱怨它无法推断模板参数Func.如何解决这个问题,知道args除了函数指针之外的任何类型.

c++ templates variadic c++11

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

避免在编译时错误条件下调用函数

我希望能够避免在条件为false时调用函数,这在编译时已知.现在我使用这样的东西:

template<bool Enabled>
void fun(params)
{
    //do nothing
}

template<>
void fun<true>(params)
{
    //do something with params.
}
Run Code Online (Sandbox Code Playgroud)

params即使函数体是空的,我也不喜欢这种方法.

我想要一个解决方案,当根本没有调用该函数时,当条件为假时,不会对params进行求值(这可能在第一种情况下使用空函数进行优化,但我不能认为这是真的.编译器).

这甚至可能吗?

c++ template-function c++11 static-if

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