小编Plu*_*luc的帖子

Git颜色合并/ rebase冲突

我想知道是否有人在发生冲突时为合并或变换的输出着色.我想特别为带有文件名的行着色,例如第二行:

Auto-merging CMakeLists.txt
CONFLICT (content): Merge conflict in CMakeLists.txt
Failed to merge in the changes.
Run Code Online (Sandbox Code Playgroud)

谢谢

编辑:

使用git别名和bash函数我可以这样写:

color-merge = "!f() { git merge --no-commit --stat $1| egrep --color 'CONFLICT .*|$'; }; f"
Run Code Online (Sandbox Code Playgroud)

这将为所有冲突线着色,但是:

  • 改变传递给合并的选项是不可能的
  • 要跟踪的分支机构没有完成

所以我正在寻找更强大的东西.

干杯

git

15
推荐指数
2
解决办法
1683
查看次数

在OpenCV中使用地址清理程序

我正在尝试将Google的Address Sanitizer与CUDA项目结合使用,更准确地说是使用OpenCV cuda功能.但是我在第一次cuda电话上遇到了"内存不足"的错误.

OpenCV Error: Gpu API call (out of memory) in getDevice, file opencv-2.4.11/src/opencv-2.4.11/modules/dynamicuda/include/opencv2/dynamicuda/dynamicuda.hpp, line 664
terminate called after throwing an instance of 'cv::Exception'
  what():  opencv-2.4.11/src/opencv-2.4.11/modules/dynamicuda/include/opencv2/dynamicuda/dynamicuda.hpp:664: error: (-217) out of memory in function getDevice
Run Code Online (Sandbox Code Playgroud)

它可以复制

#include <opencv2/gpu/gpu.hpp>
int main()
{
  cv::gpu::printCudaDeviceInfo(cv::gpu::getDevice());
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

用.编译

clang++ -fsanitize=address -lstdc++ -lopencv_gpu -lopencv_core -o sanitizer sanitizer.cpp && LD_LIBRARY_PATH=/usr/local/lib ./sanitizer
Run Code Online (Sandbox Code Playgroud)

我用gcc得到了同样的结果.我也尝试过将cuda函数列入黑名单而没有结果.

现在使用没有opencv的cuda:

#include <cuda_runtime.h>
int main()
{
  int count = -1;
  cudaGetDevice(&count);
  cout << "Device count: " << count << endl; …
Run Code Online (Sandbox Code Playgroud)

c++ opencv sanitizer clang++ address-sanitizer

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

调用两次的C++ 11 std :: generate和std :: uniform_real_distribution给出了奇怪的结果

在不同容器上从STL调用std :: generate算法两次会产生相同的结果.

考虑我想要填充两个浮点数组,其中随机数介于-1之间.和:1:

std::array<float, 1000> x;
std::array<float, 1000> y;

std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_real_distribution<float> dis(-1.f, 1.f);
auto rand = std::bind(dis, gen);
std::generate(x.begin(), x.end(), rand);
std::generate(y.begin(), y.end(), rand);
Run Code Online (Sandbox Code Playgroud)

你可以在这里测试一下:http://ideone.com/X712IU.两个数组都填充了完全相同的值:

0:  -0.411968,  -0.411968
1:    0.55158,    0.55158
2:    0.69889,    0.69889
3:  -0.901328,  -0.901328
4:  -0.556142,  -0.556142
5:  -0.798431,  -0.798431
6:  -0.570874,  -0.570874
7:   0.928999,   0.928999
8:   0.118056,   0.118056
9:  -0.655123,  -0.655123
Run Code Online (Sandbox Code Playgroud)

现在,如果我在生成之间创建一个新生成器,它可以正常工作:

std::array<float, 1000> x;
std::array<float, 1000> y;

// Generators in different scopes, OK
std::random_device rd;
{
  std::mt19937_64 …
Run Code Online (Sandbox Code Playgroud)

c++ random stl c++11

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

使用 thread_local 存储的每线程单例

这种thread_local存储持续时间的用法是否有任何警告:

template <class T>
inline T &thread_local_get()
{
  thread_local T t;
  return t;
}
Run Code Online (Sandbox Code Playgroud)

然后在不同的线程中(例如)

thread_local_get<float>() += 1.f;
Run Code Online (Sandbox Code Playgroud)

cppreference 上的文档说明了线程本地存储持续时间:

线程存储持续时间。对象在线程开始时分配,在线程结束时释放。每个线程都有自己的对象实例。只有声明为 thread_local 的对象具有此存储持续时间。thread_local 可以与 static 或 extern 一起出现以调整链接。

这是否thread_local为每个 T(编译期间)和每个调用线程正确分配了一个实例?是否有任何情况会导致例如未定义的行为?

c++ singleton thread-local thread-local-storage c++11

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

C++ 结构体隐式转换

我需要声明大量简单的 POD 结构,它们的行为相同,但实际上是不同的类型,即不是 typedef。

无论如何,我只是想让它们尽可能简单。但是在测试时我看到编译器执行一些隐式转换,我想避免这种情况。

鉴于此代码:

  template<typename T>
  struct Struct {
    T data;

    operator T() const { return data; }
  };

  void fun(Struct<float> value)
  {
    cout << "Call with Struct :: " << value << endl;
  }

  void fun(int value)
  {
    cout << "Call with INT :: " << value << endl;
  }

int main(int, char**)
{
  fun(3);
  fun(4.1f);
  fun(Struct<float>{5.2});
  fun(Struct<double>{6.3});
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

用 GCC 编译。

执行给了我:

Call with INT :: 3       // Ok
Call with INT :: 4 …
Run Code Online (Sandbox Code Playgroud)

c++ struct implicit-conversion c++11

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

C++模板类static const变量成员作为map键给出了未定义的引用

我有一堆类具有静态成员,这是一个枚举值.而且我在其他地方有一张地图,这个枚举是关键.现在,如果我在函数中使用模板参数来访问地图,我会得到一个未定义的引用.

为清楚起见,这是一个简化的非工作示例:

template<int T>
struct A
  {
    static const int Type = T;
  }

template<class T>
void fun()
  {
    cout << map_[T::Type] << endl;
  }

map<int, string> map_{{1337, "1337"}};
Run Code Online (Sandbox Code Playgroud)

主要:

fun<A<1337>();
Run Code Online (Sandbox Code Playgroud)

给了我(g ++ 4.7):

undefined reference to `(anonymous namespace)::A<1337>::Type'
Run Code Online (Sandbox Code Playgroud)

不过这个:

template<class T>
void fun()
  {
    auto key = T::Type;
    cout << map_[key] << endl;
  }
Run Code Online (Sandbox Code Playgroud)

编译和打印 1337

有人可以解释我这种行为吗?

c++ templates g++ undefined-reference c++11

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

if(!result)的C++ sugar语法返回false;

在重构一些代码时,我经常会遇到这样的问题:

bool highLevelFunc foo()
{
  // ...
  bool result = LesserLevelFunc();
    if (!result) return false;
  // ... Keep having fun if we didn't return
}
Run Code Online (Sandbox Code Playgroud)

有没有办法让这个更性感,更简洁?当然没有任何开销或陷阱.

我能想到一个宏

#define FORWARD_IF_FALSE(r) if (!r) return r;

bool highLevelFunc foo()
{
  // ...
  FORWARD_IF_FALSE(LesserLevelFunc());
  // ...
}
Run Code Online (Sandbox Code Playgroud)

有什么更好的,即没有预处理器宏?

c++ syntactic-sugar

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