小编ori*_*and的帖子

从折叠表达式创建一个带分隔符的字符串

我有以下功能:

template<typename... Args>
void Foo(Args && ... args)
{
    std::stringstream stream;
    (stream << ... << args);
    // Do somethign with it
}
Run Code Online (Sandbox Code Playgroud)

它可以完美地将 params 连接到一个字符串中,但正如预期的那样,它给出了没有任何分隔符的输出。是否有可能以某种方式分隔彼此的输入?

示例输出:

elg.Debug("asd", "qwe", 123);

// 打印:asdqwe123

// 应该打印类似的东西:asd qwe 123

我是否必须为此滚动我自己的字符串流替换?

c++ visual-c++ fold-expression c++17

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

如何使用 vscode api 编写异步代码(承诺?):withProgress

我对标题感到非常抱歉,我不知道如何更好地描述我的问题。

我想实现 VSCode 的 withProgress API,以便能够在我的代码运行/进行时显示进度条。文档在这里:https : //code.visualstudio.com/api/extension-capabilities/common-capabilities#progress-api

我试图实现它:

vscode.window.withProgress({
    location: vscode.ProgressLocation.Notification,
    title: "I am long running!",
}, (progress, token) => {
    return new Promise(resolve => {
        const output = executeProcess('sleep 5');
        resolve();
    });
});
Run Code Online (Sandbox Code Playgroud)

executeProcess(...) 是 npm child_process.spawnSync 的包装器。我需要它是同步的,因为我想阅读它的标准输出。

所以,我的问题是它当前正在运行 executeProcess,当它完成时,它开始显示进度条。我怎么能写成它首先开始显示进度条的方式,同时它正在运行并在后台完成它的工作?

是否有可能不需要重构我的代码来使用 child_process.spawn 和回调?

typescript visual-studio-code vscode-extensions

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

局部常量不是constexpr可评估的,但无法弄清楚为什么

我正在尝试将一个int作为参数并单独对其字节进行操作,例如,采用0xDEADF00D并一一处理每个字节:0xDE 0xAD 0xF0 0x0D

为此,我完成了以下代码:

template <int state, int seed>
constexpr static uint32_t CalculateRandomFromState()
{
    const char bytes[4] = {
        (state >> 24) & 0xFF,
        (state >> 16) & 0xFF,
        (state >> 8) & 0xFF,
         state & 0xFF,
    };

    constexpr auto value = Compiletime::Hash<seed, sizeof(bytes)>(bytes);

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

HashFn的信号为:

template <const uint32_t seed, const uint32_t size = NULL>
constexpr uint32_t Hash(const char* message)
Run Code Online (Sandbox Code Playgroud)

编译因以下原因而失败:

错误C2131:表达式未求值为常数

注意:失败是由于在其生命周期之外读取变量导致的

注意:请参阅“字节”的用法

我已经在StackOverflow上阅读了有关参数可能无法在编译时进行评估的主题,(这就是为什么我将大多数参数切换为模板变量的原因,因此100%确保了它们在编译时),但是在这种情况下,它不会为何给出错误似乎是合乎逻辑的。该bytes值取决于编译时间值,字节也是一个常数。

为什么会超出使用寿命?如果我说"somestring"而不是变量,bytes那么它可以完美编译。这里有什么不可以持续评估的?

c++ visual-c++ constexpr c++17

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

C++ regex_search 在 C 风格数组上进行匹配

我有一个 C 风格的数组(不一定以 null 结尾)。我想用正则表达式搜索它。我的代码如下:

const void* Search(const char* startAddress, const char* endAddress, std::regex *re)
{
    std::smatch match;
    auto ret = std::regex_search(startAddress, endAddress, *re);
Run Code Online (Sandbox Code Playgroud)

在目前的形式下,它工作得很好,但是我想知道它在哪里找到了那个特定的模式。一旦我将匹配添加为参数,编译器就无法找到合适的重载函数。我尝试在该区域之外创建一个string_view,但编译器也找不到任何适合这些迭代器的重载。

我正在专门寻找std::regex解决方案。我应该如何使用它?

c++ regex c++17

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

在 if 语句中定义的局部变量意外出现

最初我想将一堆语句打包成一行,这样我的东西就可以用作 if 中的简单宏。我需要做三件事:

  • 创建局部变量
  • 用函数更新它
  • 检查更新的变量是否匹配其他内容

这是我的快速草稿代码:

#include <iostream>
#include <stdint.h>
#define MAGIC_VALUE 42

bool MyMockedFunction(uint64_t* outElement)
{
    *outElement = MAGIC_VALUE;

    return true;
}

static const uint64_t global_should_match_this = MAGIC_VALUE;

int main()
{
    // Originally I wanted to declare the variable in one single line (as a defined macro, which should be used in an IF statement)
    // That's why I have so many things packed into the same line
    // It may or may not (latter is more probable) …
Run Code Online (Sandbox Code Playgroud)

c++ cross-platform undefined-behavior

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

Why traditional GetProcAddress to std::function is not working straightforward

As in the title I'd like to convert GetProcAddress into std::function. Yes, there are multiple solutions in stack overflow, but none actually explains why those workarounds are needed. I can't really understand the exact error message and why it happens. The sample source is simple:

#include <functional>
#include <Windows.h>

using fn_NtAllocateVirtualMemory = NTSTATUS (NTAPI*)(
        HANDLE      ProcessHandle,
        PVOID*      BaseAddress,
        ULONG_PTR   ZeroBits,
        PSIZE_T     RegionSize,
        ULONG       AllocationType,
        ULONG       Protect
    );

int main()
{
    std::function<fn_NtAllocateVirtualMemory> myFn(reinterpret_cast<fn_NtAllocateVirtualMemory>(0xDEADBABE));
}
Run Code Online (Sandbox Code Playgroud)

( https://godbolt.org/z/FhaeLA )

So, my question is …

c++ dynamic-import c++17

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