我正在使用以下钩子为我的 C++ 项目使用预提交(版本 2.20.0):
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v14.0.6
hooks:
- id: clang-format
Run Code Online (Sandbox Code Playgroud)
我只是从几个不同的 .cpp/.h/cmakelists 文件中分别暂存了几行。当我尝试提交这些更改时,我从预提交中收到以下错误:
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:\Users\tyler.shellberg\.cache\pre-commit\patch1665600217-21836.
clang-format.............................................................Failed
- hook id: clang-format
- files were modified by this hook
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
[INFO] Restored changes from C:\Users\tyler.shellberg\.cache\pre-commit\patch1665600217-21836.
Run Code Online (Sandbox Code Playgroud)
我对此感到困惑。预提交是否不允许我部分提交文件,或者在提交时根本不允许进行未暂存的更改?
所有文件,无论是暂存的还是未暂存的,都已按 clang-format 进行格式化。我已经手动仔细检查了这一点。
为了清楚起见编辑:
如果我pre-commit run --files [filename]在每个文件(暂存和未暂存)上运行,所有报告都会返回“已通过”或“已跳过”(对于非 cpp 文件)。如果所有文件都通过,为什么会出现问题?
自 2020 年 6 月以来,我一直无法使用pacman -Syu. 当我尝试时,出现以下错误:
(它开始“检查包完整性”,然后为每个包显示类似这样的错误)
error: gcc-libs: signature from "David Macek <david.macek.0@gmail.com>" is unknown trust
:: File /var/cache/pacman/pkg/gcc-libs-10.2.0-1-x86_64.pkg.tar.zst is corrupted (invalid or corrupted package (PGP signature)).
Do you want to delete it? [Y/n]
Run Code Online (Sandbox Code Playgroud)
现在,有用的是,MSYS2 确实有一篇文章解释了为什么会发生这种情况以及如何修复它:https://www.msys2.org/news/#2020-06-29-new-packagers
然而,尽管遵循了所有这些步骤,但没有任何改变,并且我遇到了相同的错误。
为了清楚起见,我做了以下操作:
curl -O http://repo.msys2.org/msys/x86_64/msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz
curl -O http://repo.msys2.org/msys/x86_64/msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz.sig
pacman-key --verify msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz.sig
pacman -U msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz
Run Code Online (Sandbox Code Playgroud)
这些“有效”(没有错误)但没有解决任何问题,所以我尝试了:
rm -r /etc/pacman.d/gnupg/
pacman-key --init
pacman-key --populate msys2
Run Code Online (Sandbox Code Playgroud)
但这也没有取得任何成果。
我该怎么办?
IE浏览器,这个:
if (x > 5)
return test;
Run Code Online (Sandbox Code Playgroud)
总是会变成:
if (x > 5)
{
return test;
}
Run Code Online (Sandbox Code Playgroud)
我不是在谈论大括号样式(Allman、GNU、Whiteman 等),我只是说完全有大括号。
有一些东西可以防止/启用单行控制语句,例如:
if (x > 5) return test;
这是AllowShortBlocksOnASingleLine,但这不是我在这里寻找的东西。
如果它适用于 clang 7 那是理想的,但如果不是让我知道。
以下是一些可以减少日常重复的事情:
param节的数量与其下面的函数声明中的参数数量不匹配[in],[out]或者[in,out]。理想情况下,自动填充任何标记const为[in]\。@ /**
* \brief Given a position in the grid, this updates the per-code bounds.
* \param[in] code - The code we are adjusting the bounds of
* \param[in] position1 - The new lower position
* \param[in] position2 - The position of the lower cell within the high-level cell,
* this has a …Run Code Online (Sandbox Code Playgroud) 以下是要点:
1:该项目使用开源存储库NanoRT作为项目中的光线追踪器。
2:本项目在Visual Studio 2019(C++17)下编译
3:该项目编译时带有被视为错误的警告(并且无法更改)。添加定义来抑制这些警告并没有帮助。
然而,看起来此代码的一部分在 C++17 中不起作用,因为它使用了已弃用的内容:(我提供的链接中的第 133 行和第 134 行)
typedef typename std::allocator<T>::pointer pointer;
typedef typename std::allocator<T>::size_type size_type;
Run Code Online (Sandbox Code Playgroud)
我需要弄清楚如何解决这个问题。该错误建议使用std::allocator_traits,但我真的不熟悉std::allocatoror的这种用法allocator_traits。
查看源代码,这是一个几行修复,还是比这更复杂并且需要重构大部分代码?
看起来pointer被用作 的返回值allocate()和 的第一个参数deallocate(),并size_type以相同的方式使用。
// Actually do the allocation. Use the stack buffer if nobody has used it yet
// and the size requested fits. Otherwise, fall through to the standard
// allocator.
pointer allocate(size_type n, void *hint = 0) {
if …Run Code Online (Sandbox Code Playgroud) 假设我有这门课
class Point
{
inline float x() const { return v[0]; }
inline float y() const { return v[1]; }
inline float z() const { return v[2]; }
float v[3];
};
Run Code Online (Sandbox Code Playgroud)
我这样做:
Point myPoint;
myPoint[0] = 5;
// unrelated code goes here
float myVal = myPoint.x() + 5;
Run Code Online (Sandbox Code Playgroud)
将上的GCC-O2或-O3优化掉任何调用x()与刚开v[0]?IE:
float myVal = myPoint.v[0] + 5;
Run Code Online (Sandbox Code Playgroud)
或者有什么理由说明这是不可能的?
更新:应该提到我确实意识到inline对编译器的建议比其他任何事情都重要,但无论如何都想知道。
作为一个附加问题,对此类进行模板化是否会对可以进行的优化产生任何影响?
我们希望在git上有每个开发人员的分支。换句话说,开发人员可以从“开发”分支中分支出来,并创建自己的本地“功能”分支来进行工作。
当他们对工作感到满意时,可以切换到devel,确保它们具有最新文件,然后合并到其“功能”分支中,并在解决任何冲突后将结果推送到原始位置。但是,有一个问题-这使得提交历史非常混乱。
如果这样做git log --graph,“功能”分支不会显示,这就是我们想要的。但是,开发人员现在进行的每个提交都会显示在devel分支上。太丑了 如果有10个开发人员,并且每个开发人员在全部合并到devel中之前对他们的功能分支进行了30次提交,那么devel现在将显示180次提交的历史,共6个功能。如果历史记录仅显示6的消息合并为devel,那就更好了。
有什么方法可以避免合并时使历史变得“混乱”并在合并时忽略本地分支的历史?还是这只是与git对抗过多?
我意识到这听起来有些奇怪,但是他们非常不喜欢所有这些对象最终存储在远程仓库中的想法。
我看到了如何在C ++ 11中执行此操作的示例:
std::unique(v.begin(), v.end(), [](float l, float r)
{
return std::abs(l - r) < 0.01;
});
Run Code Online (Sandbox Code Playgroud)
但是,这对于我在C ++ 03中失败:
error: template argument for 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate)' uses local type 'CRayTracer::myFunc()::<lambda(float, float)>'
Run Code Online (Sandbox Code Playgroud)
如何在C ++ 03中做到这一点?我认为Lambda可能已经存在,并且函子/函数对象已经存在,对吗?只是寻找一个简单的解决方案,并不需要是可扩展的-它只会在这里使用。
这是无法为我编译的代码示例:
error: template argument for 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate)' uses local type 'CRayTracer::myFunc()::<lambda(float, float)>'
Run Code Online (Sandbox Code Playgroud)
这是它产生的错误:
testUnique.cpp: In function 'int main()':
testUnique.cpp:21:37: error: no matching function for call to 'unique(std::vector<float>::iterator, std::vector<float>::iterator, main()::approx_equal&)'
21 | std::unique(v.begin(), v.end(),f);
| …Run Code Online (Sandbox Code Playgroud) 我问这个是因为关于 SO 的其他相关问题似乎是针对旧版本的 C++ 标准,没有提到任何形式的并行化,或者专注于在删除元素时保持排序/索引相同。
我有一个可能包含数十万或数百万个元素的向量(它们是相当轻的结构,假设它们被压缩了大约 20 个字节)。
由于其他限制,它必须是 astd::vector并且其他容器不起作用(例如std::forward_list),或者在其他用途中甚至不太理想。
我最近从简单的it = std::erase(it)方法切换到使用 pop-and-swap 使用这样的东西:
for(int i = 0; i < myVec.size();) {
// Do calculations to determine if element must be removed
// ...
// Remove if needed
if(elementMustBeRemoved) {
myVec[i] = myVec.back();
myVec.pop_back();
} else {
i++;
}
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,并且是一个显着的改进。它将方法的运行时间减少到以前的 61%。但我想进一步改进这一点。
C++ 是否有一种方法可以std::vector有效地从 a 中删除许多非连续元素?就像将索引向量传递给erase()C++ 并让 C++ 在幕后做一些魔术以最大程度地减少数据移动?
如果是这样,我可以让线程单独收集必须并行删除的索引,然后组合它们并将它们传递给擦除()。
c++ ×4
clang-format ×2
git ×2
accessor ×1
allocator ×1
braces ×1
c++03 ×1
c++11 ×1
c++17 ×1
c++20 ×1
curly-braces ×1
doxygen ×1
formatting ×1
functor ×1
gcc ×1
git-branch ×1
git-merge ×1
lambda ×1
msys2 ×1
optimization ×1
pacman ×1
stdvector ×1
whitespace ×1
windows ×1