小编Pao*_*oni的帖子

std :: unique_ptr用于需要免费的C函数

想想一个返回必须为freed的东西的C函数,例如POSIX strdup().我想在C++ 11中使用该函数并避免任何泄漏,这是正确的方法吗?

#include <memory>
#include <iostream>
#include <string.h>

int main() {
    char const* t { "Hi stackoverflow!" };
    std::unique_ptr<char, void(*)(void*)>
        t_copy { strdup(t), std::free };

    std::cout << t_copy.get() << " <- this is the copy!" <<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

假设它有意义,可以使用与非指针相似的模式吗?例如,POSIX的函数open返回int

c++ c++11

50
推荐指数
5
解决办法
9886
查看次数

如果我在标题中需要匿名命名空间怎么办?

在C++中,匿名命名空间等同于:

namespace $$$$ {
  //something
}
using namespace $$$$;
Run Code Online (Sandbox Code Playgroud)

其中$$$$是某种唯一标识符.然后,匿名命名空间对于不应在编译单元外看到的代码很有用.

到目前为止一直很好,但是最近我开始用模板编写一些代码,这样的代码必须在头文件中,因此使用匿名命名空间没有多大意义,因为仅仅包含头将使隔离效果无效.

那么问题是,在这种情况下建议的方式是什么?我开始使用名为Private的命名空间.它并没有真正阻止任何想要在里面使用标识符的人,但至少它将名称冲突减少为id"Private".

还有更好的方法吗?建议?

c++ namespaces anonymous header-files

11
推荐指数
2
解决办法
3207
查看次数

xrandr相关,C编程

以下是对xrandr的示例调用:

$ xrandr --output LVDS --mode 1680x1050 --pos 0x0 --rotate normal --output S-video --off --output DVI-0 --mode 1024x768 --pos 1680x104 --rotate normal

想想一个呼叫成功的系统; 有两个屏幕(LVDS和DVI-0)使用不同的分辨率.DVI-0在右侧放置在中间.

如何在C程序中获取所有这些信息?我检查了xrandr源代码,但我发现很难阅读,并且没有明显的方法来查询--pos值(编辑:它隐藏在明显的视线中,感谢ernestopheles的回答我得到了它).

我知道我可以用XGetWindowProperty问一个_NET_WORKAREA,但据我所知,它并没有告诉屏幕位置,只是包含它们的理想矩形的大小.

在对xrandr代码进行了一些其他研究之后,这段代码似乎向前迈进了一步.但我不相信,第2940行的xrandr.c假定crtc_info可能不可用.我仍然想念获得分辨率和位置的另一种方式.


    #include <stdio.h>
    #include <X11/extensions/Xrandr.h>

    int main() {
        Display *disp;
        XRRScreenResources *screen;
        XRROutputInfo *info;
        XRRCrtcInfo *crtc_info;
        int iscres;
        int icrtc;

        disp = XOpenDisplay(0);
        screen = XRRGetScreenResources (disp, DefaultRootWindow(disp));
        for (iscres = screen->noutput; iscres > 0; ) {
            --iscres;

            info = XRRGetOutputInfo (disp, screen, screen->outputs[iscres]);
            if (info->connection == RR_Connected) {
                for (icrtc = …

c x11 screen xrandr

10
推荐指数
2
解决办法
3800
查看次数

使用set -e解决程序退出状态的一般解决方案

我现在正在使用Bash进行编程,set -e因为在程序失败时继续执行脚本几乎不是想要的行为.

如果是我使用,||true如果我不需要退出代码.

如果我需要退出代码,我将执行包装如下:

set +e
call_I_need_the_exit_code with few arguments
RV="$?"
set -e
# use "$RV" somewhat
Run Code Online (Sandbox Code Playgroud)

但是,它很冗长,我很少切换set +eset -e引入恼人的错误.

有没有办法创建一个执行命令的函数,并为退出代码设置一个已知变量?

像这样的东西(伪代码):

safe_call( call_I_need_the_exit_code(with, few, arguments) )
# use "$RV" somewhat
Run Code Online (Sandbox Code Playgroud)

其中safe_call基本上是前一段代码.它会使我的代码更容易编写和阅读......

bash exit-code

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

什么是"延迟"构建C++对象的最惯用的方法是什么?

对于内置类型,如int,您可以简单地写入任何内容来延迟初始化.有没有办法对C++对象做同样的事情?

我写了这个代码来完成这项工作,但我想知道是否有一种惯用的方法.如果是这样,它是什么?甚至在引入对齐存储之前是否可能?

#include <utility>
#include <type_traits>

template <typename T>
struct delayed {
    delayed() { unset_init(); }
    template <typename...Args> void init(Args&&... args) {
        new ( memory() ) T(std::forward<Args>(args)...);
        set_init();
    }

    operator T*() {
        return memory();
    }

    ~delayed() {
        if (get_init()) {
            memory()->~T();
            unset_init();
        }
    }

private:
    T* memory() { return reinterpret_cast<T*>(&bytes_); }
    unsigned char* raw_memory() { return reinterpret_cast<unsigned char*>(&bytes_); }

    unsigned char& init() { return *( raw_memory() + sizeof(T) ); }
    bool get_init() { return init() != 0; }
    void set_init() …
Run Code Online (Sandbox Code Playgroud)

c++ constructor c++11

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

如果我缩短最佳TSP解决方案,仍然是最佳的?

让我们有一个包含k个节点的完整无向度量图; 度量图是满足三角不等式的图,因此对于所有节点a,b,c,权重函数w(a,c)小于或等于w(a,b)+ w(公元前).

Wlog让我们说循环:<1,2,3,...,k,1>是该图的最佳TSP解决方案.

我的问题是:如果我从图中删除一个节点(例如第n个)并且我快速跳过循环,那么结果循环仍然是最佳的TSP解决方案吗?

nb,循环将变为<1,2,...,n-1,n + 1,...,k,1>

algorithm graph traveling-salesman

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

通过环境变量选择默认的 alsa PCM(非卡)

我经常使用耳机,选择是使用耳机还是普通声卡我使用环境变量,这要归功于 ~/.asound.rc 中的以下代码:

@args.CARD {
  type string
  default {
    @func getenv
    vars {
      0 ALSA_CARD
    }
    default {
      @func refer
      name 'defaults.pcm.card'
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,新耳机 (hw:Set) 的频道颠倒了,所以我创建了一个 pcm 来修复,将这些行添加到 ~/.asoundrc:

pcm.swapped {
    type  route
    slave {
        pcm "hw:Set"
    }
    ttable.0.1   1
    ttable.1.0   1
}


pcm.HeadsetSwapped {
  type plug
  slave {
    pcm "pcm.swapped"
  }
}
Run Code Online (Sandbox Code Playgroud)

pcm 有效,但我如何决定通过环境变量使用它?目前 env 变量只接受卡片名称。它可能与 -- name 'defaults.pcm.card' -- 行有关,但我无法修复它。

我知道我可以将 pcm.HeadsetSwapped 更改为 pcm.!default,但我想避免每次要启动程序时都编辑文件...

linux audio alsa

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

std :: function可以带函子吗?

我尝试了这一小段代码,令我惊讶的是我的编译器不喜欢它.

如果我删除write_by_call(h),它会按预期工作; 如果我离开它,它就不会编译,因为它知道没有从h(匿名类)到第一个参数的std :: function的转换.

这是预期的吗?有谁知道关于std :: functions和functor的标准是什么?

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

void write_by_call(std::function<std::string ()> return_str_f) {
    if (return_str_f) {
        std::cout << return_str_f() << std::endl;
    } else {
        std::cout << "I do not like this one..." << std::endl;
    }
}

class {
    std::string operator()() {
        return std::string("hi, I am the class h");
    }
} h;


std::string f() {
    return std::string("hi, I am f");
}

auto g = []() { return std::string("I am from the lambda!"); };

int main() …
Run Code Online (Sandbox Code Playgroud)

function functor c++11

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

关于 C++ 无效引用,创建或使用是否会导致程序格式错误?

看这段小代码:

std::deque<BIG> bigs {};
// add some BIGs in the deque

BIG& last_BIG { bigs.front() };
f(last_BIG);
bigs.pop_front();
g();
Run Code Online (Sandbox Code Playgroud)

调用后pop_front引用last_BIG无效,这是否足以使程序格式错误?换句话说,我是否必须放入last_BIG更小的范围?

当然,弹出后使用是未定义的。 last_BIG

c++ reference c++11

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