小编MHe*_*bes的帖子

WinMerge - 忽略所有空格,包括换行符和格式更改

使用 WinMerge 作为 difftool 时如何忽略“代码样式”更改?特别是跨越两次提交。

以便

thing
{
  a,
  b
}
Run Code Online (Sandbox Code Playgroud)

thing { a, b }
Run Code Online (Sandbox Code Playgroud)

将被视为相同。

本质上是这个问题,但针对的是 winmerge 而不是 diff。

.gitconfig

[diff]
    tool = winmerge
[difftool]
    prompt = false
[difftool "winmerge"]
    cmd = "$HOME/scripts/winmerge.sh" "$LOCAL" "$REMOTE"
[mergetool]
    prompt = false
    keepBackup = false
    keepTemporaries = false
[merge]
    tool = winmerge
[mergetool "winmerge"]
    cmd = "'C:/Program Files (x86)/WinMerge/WinMergeU.exe'" -e -u -fm -dl \"Local\" -dr \"Remote\" "$LOCAL" "$MERGED" "$REMOTE"
Run Code Online (Sandbox Code Playgroud)

winmerge.sh只是打电话WinMergeU.exe -e -u -wr "$1" …

git winmerge

14
推荐指数
2
解决办法
2万
查看次数

如何在 Win32 中创建子进程,以便它们在任务管理器中显示为嵌套的?

我有一个 Win32 C++ 应用程序。我正在尝试使用 启动一个或多个子进程CreateProcess。我希望孩子们在父母关闭时关闭。

我通过创造一份工作并启用以下功能来实现这一目标JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE

HANDLE hJob = CreateJobObject(NULL, NULL);

JOBOBJECT_EXTENDED_LIMIT_INFORMATION extendedInfo;
ZeroMemory(&extendedInfo, sizeof(extendedInfo));
extendedInfo.BasicLimitInformation.LimitFlags =
    JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;

SetInformationJobObject(
        hJob, JOBOBJECTINFOCLASS::JobObjectExtendedLimitInformation,
        &extendedInfo, sizeof(extendedInfo));
Run Code Online (Sandbox Code Playgroud)

然后将当前(父)和创建(子)进程添加到该作业中:

// assign parent to job
AssignProcessToJobObject(hJob, GetCurrentProcess());

// launch child with no inherited handles
PROCESS_INFORMATION procInfo;
ZeroMemory(&procInfo, sizeof(procInfo));
STARTUPINFOA startInfo;
ZeroMemory(&startInfo, sizeof(startInfo));
startInfo.cb = sizeof(startInfo);
startInfo.dwFlags |= STARTF_USESTDHANDLES;
bool success = CreateProcessA(NULL,
                              "test.exe",  // command line
                              NULL,     // process security attributes
                              NULL,   // primary thread security attributes
                              FALSE,  // handles are inherited …
Run Code Online (Sandbox Code Playgroud)

c++ windows winapi

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

为什么这个 lambda [=] 捕获会创建多个副本?

在下面的代码中:

#include <iostream>
#include <thread>

using namespace std;

class tester {
public:
    tester() { 
        cout << "constructor\t" << this << "\n"; 
    }
    tester(const tester& other) { 
        cout << "copy cons.\t" << this << "\n"; 
    }
    ~tester() { 
        cout << "destructor\t" << this << "\n"; 
    }

    void print() const { 
        cout << "print\t\t" << this << "\n"; 
    }
};

int main() {
    tester t;

    cout << "  before lambda\n";
    thread t2([=] {
        cout << "  thread start\n";
        t.print();
        cout << " …
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++17

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

如何使用 std::chrono 查找当前工作日?

我只是想使用 C++20 std::chrono 打印当前工作日(当地时间)。

看起来很简单():

#include <chrono>
#include <iostream>

int main() {
  using namespace std::chrono;

  system_clock::time_point now = system_clock::now();
  // system_clock::time_point now = sys_days{June / 26d / 2023y} + 12h;

  weekday wd{floor<days>(now)};

#ifdef _WIN32
  std::cout << wd << "\n";
#else
  std::cout << wd.c_encoding() << " (Sunday = " << Sunday.c_encoding() << ")\n";
#endif

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

然而,这Tue对我来说是打印出来的(今天是星期一)。如果我将评论切换到手动定义的日期(此问题发布于 2023 年 6 月 26 日 -0400),它会打印Mon.

  1. 如何在当前日期正确执行此操作?

  2. 为什么now()日期的行为与sys_days/ …

c++ c++-chrono c++20

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

std::format %z 说明符不适用于 local_time

我想获取格式为“YYYY-MM-DD hh::mm::ss.fff +zz”的时间戳。

我以为我可以使用%z说明符 forstd::format来执行此操作,但我在 MSVC 上收到此错误:

error C7595: 'std::basic_format_string<char,const std::chrono::time_point<
std::chrono::local_t,std::chrono::duration<std::chrono::system_clock::rep,std::chrono::system_clock::period>> &>::basic_format
_string': call to immediate function is not a constant expression
Run Code Online (Sandbox Code Playgroud)
error C7595: 'std::basic_format_string<char,const std::chrono::time_point<
std::chrono::local_t,std::chrono::duration<std::chrono::system_clock::rep,std::chrono::system_clock::period>> &>::basic_format
_string': call to immediate function is not a constant expression
Run Code Online (Sandbox Code Playgroud)

如果我用作"{:%F %T}"格式字符串,它工作正常(但显然不包括时区)。这告诉我该错误消息是无意义的,但是有什么办法可以解决这个问题吗?这是编译器中的错误吗?


对于后代,这里有一个使用 good old 的解决方法strftime

#include <chrono>
#include <format>
#include <iostream>

std::string get_current_time_and_date() {
  auto const time =
      std::chrono::current_zone()->to_local(std::chrono::system_clock::now());
  return std::format("{:%F %T %z}", time);
}

int main() {
  std::cout << get_current_time_and_date() << …
Run Code Online (Sandbox Code Playgroud)

c++ c++20 fmt

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

C++ 无法为 lambda 中捕获的 Promise 调用 set_value ?

我正在尝试编写一个相当简单的方法来返回未来。lambda 决定了未来。这是一个最小的例子。实际上,lambda 可能会在不同的线程中调用,等等。

#include <future>

std::future<std::error_code> do_something() {
  std::promise<std::error_code> p;
  auto fut = p.get_future();
  auto lambda = [p = std::move(p)] {
    std::error_code error;
    p.set_value(error);
  };
  lambda();
  return std::move(fut);
}

int main() { return do_something().get().value(); }
Run Code Online (Sandbox Code Playgroud)

由于某种原因,我收到类型错误。VSCode 智能感知 说:

没有重载函数“ std::promise<_Ty>::set_value [with _Ty=std::error_code]”的实例与参数列表和对象匹配(该对象具有阻止匹配的类型限定符) -- 参数类型为: ( std::remove_reference_t<std::error_code &>) -- 对象类型为:const std::remove_reference_t<std::promise<std::error_code> &>

MSVC 编译器说:

错误 C2663: ' std::promise<std::error_code>::set_value': 2 个重载没有对 ' this' 指针进行合法转换

我真的不明白 VS Code 错误。它是说它认为error是 aconst promise<error_code>吗?如何正确调用lambda 捕获中的set_value承诺? …

c++ boost-asio c++11 std-future

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

如何从 git bash 使用命令行 Visual Studio 编译器?

与这个问题相关。

cl.exe我想在 Windows 上从 git bash运行。通常您需要开发人员提示才能执行此操作,因此我这样做:

cmd //V //K "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat"
Run Code Online (Sandbox Code Playgroud)

这会将开发者提示符作为子 shell 打开。如果我愿意的话,我可以从那里逃跑cl

但是,如果我可以从 git bash 运行命令而无需进入子 shell,那就太好了。我会很高兴:

  1. 以某种方式“ sourceing”文件VsDevCmd.bat以导出它设置为环境中变量的变量,这样我就可以cl从 bash 运行,或者

  2. 将 更改//K为 a//C并更改为当前(bash)工作目录,然后cl使用转发的任何参数执行。如果它是别名,则奖励积分。

根本无法弄清楚#1。

我需要 #2 语法方面的帮助,因为 while

cmd //V //C "C:\Program Files (x86)\...\VsDevCmd.bat"
Run Code Online (Sandbox Code Playgroud)

工作的意思是它执行批处理脚本并立即退出,我不知道如何将多个命令链接在一起。跑步

cmd //V //C "C:\Program Files (x86)\...\VsDevCmd.bat & cl"
Run Code Online (Sandbox Code Playgroud)

例如,给出

'C:\Program' is not recognized as an internal or external command,
operable program or batch file. …
Run Code Online (Sandbox Code Playgroud)

bash cmd batch-file git-bash

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

创建 RAII 类时应该如何处理错误?

如果例如 RAII 套接字包装器的构造函数的资源分配部分失败,我是否只是抛出异常并完成它?或者我应该怎么std::fstream,在is_open()构造对象后需要检查的地方?

前者似乎更符合“资源分配即初始化”这个名称,但那为什么标准库基本上会让你在使用对象之前检查错误代码呢?

我指的是参考页面上的示例basic_fstream(解释,添加注释):

int main() {
  std::string filename = "test.bin";
  std::fstream s(filename);

  if (!s.is_open()) { // should my socket class require the user to do this?
    std::cout << "failed to open " << filename << '\n';
  } else {
    // ... use it
  }
}
Run Code Online (Sandbox Code Playgroud)

c++ raii

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

标签 统计

c++ ×6

c++20 ×2

bash ×1

batch-file ×1

boost-asio ×1

c++-chrono ×1

c++11 ×1

c++17 ×1

cmd ×1

fmt ×1

git ×1

git-bash ×1

lambda ×1

raii ×1

std-future ×1

winapi ×1

windows ×1

winmerge ×1