小编Hao*_*shu的帖子

通过模板参数列表区分模板函数

我创建了4个类:

class C1 {};
class C2 {};
class C3 {};
class C4 {};
Run Code Online (Sandbox Code Playgroud)

以及基于这些类的模板函数:

template<class T1, class T2>
T2 F() {
    std::cout << "F<T1, T2>" << std::endl;
    return T2();
}

template<class T>
T F();

template<>
C1 F<C1>() {
    std::cout << "F<C1>" << std::endl;
    return F<C3, C1>();
}

template<>
C2 F<C2>() {
    std::cout << "F<C2>" << std::endl;
    return F<C4, C2>();
}
Run Code Online (Sandbox Code Playgroud)

这给了我编译错误:

explicit specialization 'C1 F<C1>(void)' is not a specialization of a function template
explicit specialization 'C2 F<C2>(void)' is not a …
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

powershell的并行foreach最多使用5个线程吗?

参数throttlelimit可以foreach -parallel控制执行脚本时使用多少个进程。但即使我设置的进程数大于 5,我也不能拥有throttlelimit超过 5 个进程。

该脚本在多个 powershell 进程中执行。所以我检查脚本内的PID。然后对 PID 进行分组,以便我可以知道有多少个进程用于执行该脚本。

function GetPID() {
    $PID
}

workflow TestWorkflow {
    param($throttlelimit)
    foreach -parallel -throttlelimit $throttlelimit ($i in 1..100) {
        GetPID
    }
}

foreach ($i in 1..8) {
    $pids = TestWorkflow -throttlelimit $i
    $measure = $pids | group | Measure-Object
    $measure.Count
}
Run Code Online (Sandbox Code Playgroud)

输出是

1
2
3
4
5
5
5
5
Run Code Online (Sandbox Code Playgroud)

对于$i小于或等于5,我有$i流程。但对于$i超过 5 个进程,我只有 5 个进程。有什么办法可以增加执行脚本时的进程数吗?

编辑:为了回应@SomeShinyObject的答案,我添加了另一个测试用例。这是@SomeShinyObject 给出的示例的修改。我添加了一个函数S,它除了休眠 10 …

powershell parallel.for powershell-workflow

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

是否可以使用`switch -regex`获得匹配位置

Select-String -Pattern返回每个匹配的位置。例如:

$s = 'abcxyzabc' | Select-String -Pattern 'abc(?<x>.*)abc'
$s.Matches[0].Groups | Select-Object Name, Value, Index, Length
Run Code Online (Sandbox Code Playgroud)

给出:

Name Value     Index Length
---- -----     ----- ------
0    abcxyzabc     0      9
x    xyz           3      3
Run Code Online (Sandbox Code Playgroud)

whereIndexLength指定每个匹配的位置。

但是,在使用switch -regex. 例如:

switch -regex ('abcxyzabc') {
    'abc(?<x>.*)abc' {
        $matches
    }
}
Run Code Online (Sandbox Code Playgroud)

给出:

Name                           Value
----                           -----
x                              xyz
0                              abcxyzabc
Run Code Online (Sandbox Code Playgroud)

我找不到像什么Index,并Length在拿到了赛位置$matches

我还检查该类型Matches的返回Select-String -PatternSystem.Text.RegularExpressions.Match[],虽然类型$matchesswitch …

regex powershell switch-statement

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