我正在开发一个Git插件,我需要知道,当一个本地回购被改变(可提交更改),未来(可推送到远程)或后面(可以从远程拉)使用命令行.
这就是我到目前为止所做的事情:
可以提交?
如果git diff-index --name-only --ignore-submodules HEAD --返回一些东西,那么是的,有提交的更改.
可以推吗?
如果在其输出中git status -sb包含前面的单词,那么是,有提交要推送.
能拉吗?
还没有实现.
在可以提交?部分似乎工作正常.可以推吗?仅适用于主分支,这是一个很大的问题.
如何在每个分支上安全地检查git repo是否有提交更改,提交推送或需要更改git pull?
这是我想出的一点微优化好奇心:
struct Timer {
bool running{false};
int ticks{0};
void step_versionOne(int mStepSize) {
if(running) ticks += mStepSize;
}
void step_versionTwo(int mStepSize) {
ticks += mStepSize * static_cast<int>(running);
}
};
Run Code Online (Sandbox Code Playgroud)
这两种方法似乎实际上做了同样的事情.第二个版本是否避免了分支(因此,比第一个版本更快),或者是否有任何编译器能够进行这种优化-O3?
我想实现一个has_no_duplicates<...>类型特征,评估std::true_type传递的可变参数类型列表是否没有重复类型.
static_assert(has_no_duplicates<int, float>{}, "");
static_assert(!has_no_duplicates<float, float>{}, "");
Run Code Online (Sandbox Code Playgroud)
让我们假设,对于这个问题的范围,我想使用多重继承来做到这一点.
当一个类多次从同一类型继承时,会发生错误.
template<class T>
struct type { };
template<class... Ts>
struct dup_helper : type<Ts>... { };
// No errors, compiles properly.
dup_helper<int, float> ok{};
// Compile-time error:
// base class 'type<float>' specified more than once as a direct base class
dup_helper<float, float> error{};
Run Code Online (Sandbox Code Playgroud)
我以为我已经习惯于void_t"检测"这个错误,但是我无法在cppreference的代码示例之后实现一个有效的解决方案.
这是我试过的:
template<class, class = void>
struct is_valid
: std::false_type { };
// First try:
template<class T>
struct is_valid<T, std::void_t<decltype(T{})>>
: std::true_type …Run Code Online (Sandbox Code Playgroud) 我想我发现lambdas和可调用对象之间的另一个"clang vs gcc"不一致.
decltype(l)::operator()应该等效于C::operator(),但如果variadic pack在泛型lambda中保留为空,则gcc拒绝编译:
15:错误:对'(main()::)(int)'l(1)的调用不匹配;
15:注意:候选人:decltype(((main()::) 0u).main()::(x,))(*)(auto:1 &&,auto:2 &&,...)
15:注意:候选人需要3个参数,2个提供
14:注意:候选人:模板main()::
auto l = [](auto && x,auto && ...){return x; };
14:注意:模板参数扣除/替换失败:
15:注意:候选人需要2个参数,1个提供
升(1);
struct C
{
template<typename T, typename... TRest>
auto operator()(T&& x, TRest&&...){ return x; }
};
int main()
{
// Compiles both with clang and gcc.
auto c = C{};
c(1);
// Compiles with clang 3.7.
// Does not compile with gcc 5.2.
auto l = [](auto&& x, auto&&...) …Run Code Online (Sandbox Code Playgroud) 我想使用lambdas和重载创建函数(例如)访问"递归" .std::variantboost::hana::overload
假设我有一个变量类型my_variant,可以存储一个a int,a float或a vector<my_variant>:
struct my_variant_wrapper;
using my_variant =
std::variant<int, float, std::vector<my_variant_wrapper>>;
struct my_variant_wrapper
{
my_variant _v;
};
Run Code Online (Sandbox Code Playgroud)
(我正在使用包装my_variant_wrapper类来递归地定义变体类型.)
我想以递归方式访问变体,根据存储的类型打印不同的东西.这是一个使用基于访问者的工作示例struct:
struct struct_visitor
{
void operator()(int x) const { std::cout << x << "i\n"; }
void operator()(float x) const { std::cout << x << "f\n"; }
void operator()(const std::vector<my_variant_wrapper>& x) const
{
for(const auto& y : x) std::visit(*this, y._v);
}
};
Run Code Online (Sandbox Code Playgroud)
std::visit使用上述访问者调用打印所需的输出: …
#include <utility>
template <typename P, typename F>
struct foo
{
P _p;
int i{0};
foo(P&& p, F) : _p{std::move(p)} { }
template <typename... Cs>
void up(Cs&... cs)
{
if constexpr(sizeof...(Cs) == 2) { down(cs...); }
else { _p.up(*this, cs...); }
}
void down() { --i; }
template <typename C, typename... Cs>
void down(C& c, Cs&... cs)
{
[&] { [&] { c.down(cs...); }(); }();
}
};
int main()
{
auto f = foo{foo{foo{0, 0}, 0}, 0};
f.up();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码片段:
使用g …
首先,一些背景:我使用的是空的struct所谓nothing效仿类似的东西"常规void"为了美化一些接口依靠链接多个函数对象在一起.
struct nothing { };
Run Code Online (Sandbox Code Playgroud)
用法示例:
when_all([]{ return 0; }, []{ }, []{ return 'a'; })
.then([](int, char){ }); // result of lambda in the middle ignored
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,所发生的情况是,我包装传递给函数的对象的所有结果when_all中的std::tuple,转换void到nothing (在这个例子中:std::tuple<int, nothing, char>),然后我用被称为辅助函数apply_ignoring_nothing,通过调用函数对象解包std::tuple,忽略那些元素nothing.
auto f_then = [](int, char){ };
auto args = std::tuple{0, nothing{}, 'a'};
apply_ignoring_nothing(f_then, args); // compiles
Run Code Online (Sandbox Code Playgroud)
apply_ignoring_nothing是以实施的方式实施的call_ignoring_nothing.
我有一个call_ignoring_nothing具有以下签名的函数:
template …Run Code Online (Sandbox Code Playgroud) c++ templates metaprogramming template-meta-programming c++17
请考虑以下代码段:
#include <iostream>
#include <sstream>
int main()
{
std::stringstream ss;
ss << "12345";
unsigned short s;
ss >> s;
ss << "foo";
std::cout << std::boolalpha
<< "\nss.eof() = " << ss.eof()
<< "\nss.good() = " << ss.good()
<< "\nss.bad() = " << ss.bad()
<< "\nss.fail() = " << ss.fail()
<< "\nss.str() = " << ss.str();
}
Run Code Online (Sandbox Code Playgroud)
clang ++ trunk打印出以下结果:
Run Code Online (Sandbox Code Playgroud)ss.eof() = true ss.good() = false ss.bad() = false ss.fail() = false ss.str() = 12345
g ++ trunk打印以下结果:
Run Code Online (Sandbox Code Playgroud)ss.eof() = …
在C++ 11中,SFINAE很容易判断表达式是否有效.举个例子,假设检查某些东西是否可流动:
template <typename T>
auto print_if_possible(std::ostream& os, const T& x)
-> decltype(os << x, void());
Run Code Online (Sandbox Code Playgroud)
print_if_possible将只参加重载解析如果os << x是合式表达.
我需要在C++ 03中做同样的事情,我发现这sizeof可能有所帮助(因为我需要一个表达式的未评估上下文).这就是我想出的:
template <int> struct sfinaer { };
template <typename T>
void print_if_possible(std::ostream& os, const T& x,
sfinaer<sizeof(os << x)>* = NULL);
Run Code Online (Sandbox Code Playgroud)
似乎g ++和clang ++的最新版本都接受了这个sizeof版本-std=c++03 -Wall -Wextra.
代码是否保证在C++ 03中按预期工作?
它是正确的结论是C++ 11的表达SFINAE的任何使用可回迁C++ 03使用到sfinaer和sizeof?
请考虑以下代码:
template <int N, typename T> void f(T) { }
template <typename T>
constexpr int k(T&) { return 0; }
int main()
{
constexpr auto i = 1;
f<k(i)>([&i]
{
f<k(i)>(0);
});
}
Run Code Online (Sandbox Code Playgroud)
clang++ (主干)编译它.g++ (trunk)因以下错误而失败:
Run Code Online (Sandbox Code Playgroud)<source>: In lambda function: <source>:11:19: error: no matching function for call to 'f<k<const int>((* & i))>(int)' 11 | f<k(i)>(0); | ^ <source>:1:35: note: candidate: 'template<int N, class T> void f(T)' 1 | template <int N, typename T> void f(T) { } …