小编bil*_*ill的帖子

推导出c ++ 11中元组元素的类型

我有以下代码.

template <typename... Types>
void print_tuple(const std::tuple<Types&&...>& value)
{
    std::cout << std::get<0>(value) << "," << std::get<1>(value) << std::endl;
}
print_tuple(std::forward_as_tuple("test",1));
Run Code Online (Sandbox Code Playgroud)

哪个编译器抱怨

error: invalid initialization of reference of type ‘const std::tuple<const char (&&)[5], int&&>&’ from expression of type ‘std::tuple<const char (&)[5], int&&>’
     print_tuple(std::forward_as_tuple("test",1));
Run Code Online (Sandbox Code Playgroud)

为什么编译器会将元组中第一个元素的类型推断为const char(&&)[5]?

c++ c++11

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

如何按列方式剪辑pandas dataframe?

我有

In [67]: a
Out[67]:

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

我跑的时候

In [69]: a.clip(lower=[1.5,2.5,3.5],axis=1)
Run Code Online (Sandbox Code Playgroud)

我有

ValueError: other must be the same shape as self when an ndarray
Run Code Online (Sandbox Code Playgroud)

这是预期的吗?我期待得到类似的东西:

Out[72]:

     0    1    2
0  1.5  2.5  3.5
1  4.0  5.0  6.0
Run Code Online (Sandbox Code Playgroud)

python pandas

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

bazel 重新编译 protobuf 超过必要的程度

我按照这个例子做了,效果很好。

但有时(并非总是)当我更改一些与 protobuf 和重建完全无关的应用程序级代码时,bazel 会花费所有时间重新编译 protobuf。

有什么建议我可以尝试调试这个吗?

protocol-buffers protobuf-c bazel

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

是 std::chrono::duration 默认初始化为 0

下面的 d 值是已初始化(大概为 0)还是未初始化(读取不安全)?

std::chrono::system_clock::duration d;
Run Code Online (Sandbox Code Playgroud)

文档说默认构造函数是默认的。

下面的 std 库代码似乎表明它是未初始化的,因为最终 int64_t 是一个标量,而标量的默认初始化是没有初始化。

我的理解正确吗?std::chrono::system_clock::time_point初始化为 0 时,它让我吃惊。

    struct system_clock
    {
      typedef chrono::nanoseconds                   duration;
...
    /// nanoseconds
    typedef duration<int64_t, nano>         nanoseconds;

...
    template<typename _Rep, typename _Period>
      struct duration
      {
    typedef _Rep                        rep;
    typedef _Period                     period;

...
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++-chrono

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

在 Eigen 中实现 Clip()

我有一些代码将一些值剪辑到以 0 为中心的范围之间,如下所示。

Eigen::VectorXd a;
Eigen::VecotrXd b;
a = a.cwiseMin(b).cwiseMax(-b);  // no temporary created here?
Run Code Online (Sandbox Code Playgroud)

我想将逻辑分解为一个函数。一种解决方案:

Eigen::VectorXd Clip(const Eigen::VectorXd& a, const Eigen::VectorXd& b);

a = Clip(a, b);
Run Code Online (Sandbox Code Playgroud)

但我认为这是低效的,因为它创建了一个额外的临时?

另一种解决方案:

void Clip(Eigen::Ref<Eigen::VectorXd> a, const Eigen::VectorXd& b) {
  a = a.cwiseMin(b).cwiseMax(-b);
}
Run Code Online (Sandbox Code Playgroud)

但这有时似乎不方便使用:

void SomeFunctionSignatureICannotChange(const Eigen::VectorXd& a, const Eigen::VectorXd& b) {
  // Eigen::VectorXd a_clipped = Clip(a, b); would be cleaner.
  Eigen::VectorXd a_clipped;
  Clip(a_clipped, b);
}
Run Code Online (Sandbox Code Playgroud)

我能想到的最佳解决方案:

template <typename DerivedV, typename DerivedB>
auto Clip(const Eigen::ArrayBase<DerivedV>& v,
          const Eigen::ArrayBase<DerivedB>& bound)
          -> decltype(v.min(bound).max(-bound)) {
  return …
Run Code Online (Sandbox Code Playgroud)

c++ eigen eigen3

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

标签 统计

c++ ×3

c++11 ×2

bazel ×1

c++-chrono ×1

eigen ×1

eigen3 ×1

pandas ×1

protobuf-c ×1

protocol-buffers ×1

python ×1