小编Prz*_*ski的帖子

constexpr参数化函数指针

我有以下第三方API:

using StatisticsFunc = double (*)(const std::vector<double> &)
libraryClass::ComputeStatistics(StatisticsFunc sf);
Run Code Online (Sandbox Code Playgroud)

我正在使用这样的:

obj1->ComputeStatistics([](const auto& v) {return histogram("obj1", v);};
obj2->ComputeStatistics([](const auto& v) {return histogram("obj2", v);};
Run Code Online (Sandbox Code Playgroud)

但所有这些lambdas只是重复代码.我宁愿这样:

obj1->ComputeStatistics(getHistogramLambda("obj1"));
Run Code Online (Sandbox Code Playgroud)

所以我需要定义:

constexpr auto getHistogramLambda(const char* name) {
    return [](const auto& v) {return histogram(name, v);};
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用,因为name没有被捕获.这也不会起作用:

constexpr auto getHistogramLambda(const char* name) {
    return [name](const auto& v) {return histogram(name, v);};
}
Run Code Online (Sandbox Code Playgroud)

因为捕获lambda不再是无状态的,不能转换为函数指针.

Ofc可以将其作为宏来实现,但我想要一个现代的C++ 17解决方案.

传递字符串作为模板参数似乎也是一个选项:https: //stackoverflow.com/a/28209546/7432927,但我很好奇是否有constexpr办法做到这一点.

c++ lambda constexpr c++17

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

使用std :: generate的随​​机unordered_multimap

我正在尝试unordered_multimap使用以下代码生成大小为10 的随机数:

#include <algorithm>
#include <unordered_map>
#include <cstdlib>

int main()
{
    auto m = std::unordered_multimap<int, int>(10);
    std::generate(
        m.begin(),
        m.end(),
        [](){return std::pair{std::rand(),std::rand()};}
    );
} 
Run Code Online (Sandbox Code Playgroud)

但它不会编译错误

 In file included from /usr/include/c++/7/algorithm:62:0,
                 from main.cpp:2:
/usr/include/c++/7/bits/stl_algo.h: In instantiation of ‘void std::generate(_FIter, _FIter, _Generator) [with _FIter = std::__detail::_Node_iterator<std::pair<const int, int>, false, false>; _Generator = main()::<lambda()>]’:
<span class="error_line" onclick="ide.gotoLine('main.cpp',11)">main.cpp:11:5</span>:   required from here
/usr/include/c++/7/bits/stl_algo.h:4438:11: error: use of deleted function ‘std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(typename std::conditional, std::is_copy_assignable<_T2> > >::value, const std::pair<_T1, _T2>&, const std::__nonesuch_no_braces&>::type) [with _T1 = …
Run Code Online (Sandbox Code Playgroud)

c++ stl-algorithm unordered-multimap c++17

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

如何将 boost::string_view 转换为 std::string_view?

我正在使用一个 boost 库,它使用boost::string_view. 但是,我想std::string_view在我的代码中使用。
问:这两者之间转换的最佳方式是什么?

目前我正在使用:

void foo(std::string_view sv) {
# ...
}
void foo(boost::string_view bsv) {
  foo(std::string(bsv));
}
Run Code Online (Sandbox Code Playgroud)

但这会产生不必要的字符串。

c++ boost string-view c++17

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

使用模板重载运算符,但阻止重新定义

我想定义operator<<使用模板的特化,但我不希望它破坏了这个运算符的行为,如果它已经为某些数据类型定义的话.

enum State {Open, Locked};
enum Input {Coin, Push};

std::string ToString(State c){
  switch (c) {
    case Locked : return "Locked";
    case Open : return "Open";
  }
}

std::string ToString(Input c){
  switch (c) {
    case Coin : return "Coin";
    case Push : return "Push";
  }
}

template<typename T>   //obviously, a bad idea
std::ostream& operator<<(std::ostream& s, T c) {
  return s<<ToString(c);
}
Run Code Online (Sandbox Code Playgroud)

稍后在代码中我想使用:

int main() {
  std::cout<<Coin<<std::endl;
  std::cout<<Open<<std::endl;
  std::cout<<std::string("normal string")<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

不出所料,上面给出了编译错误:

error: ambiguous overload for ‘operator<<’ (operand types are …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading operator-overloading

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

禁用 Jenkinsfile 中的一个阶段

我想暂时禁用 Jenkinsfile 中的某些阶段。删除它们然后从版本历史记录中恢复似乎太麻烦了。我尝试添加这个:

    stage('Tests') {
      when {
        false
      }
    (...)
Run Code Online (Sandbox Code Playgroud)

但是当我触发作业时会导致错误:

WorkflowScript: 30: Expected a when condition @ line 30, column 7.

         when {

         ^

WorkflowScript: 30: Empty when closure, remove the property or add some content. @ line 30, column 7.

         when {
Run Code Online (Sandbox Code Playgroud)

有没有办法when: never在詹金斯声明性管道中做?

continuous-integration jenkins jenkins-pipeline jenkins-declarative-pipeline

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