假设我的git repostiory中有一个文件叫做foo.
假设它已被删除rm(不是git rm).然后git状态将显示:
Changes not staged for commit:
deleted: foo
Run Code Online (Sandbox Code Playgroud)
如何暂存此单个文件?
如果我尝试:
git add foo
Run Code Online (Sandbox Code Playgroud)
它说:
'foo' did not match any files.
Run Code Online (Sandbox Code Playgroud) 我有两个宏,FOO2并且FOO3:
#define FOO2(x,y) ...
#define FOO3(x,y,z) ...
Run Code Online (Sandbox Code Playgroud)
我想定义一个新的宏FOO如下:
#define FOO(x,y) FOO2(x,y)
#define FOO(x,y,z) FOO3(x,y,z)
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为宏不会在参数数量上超载.
无需修改FOO2和FOO3,是有一些方法来定义一个宏FOO(使用__VA_ARGS__或以其他方式),以获得分派的相同的效果FOO(x,y)来FOO2,并FOO(x,y,z)到FOO3?
我很好奇如何std:next_permutation实现,所以我提取了gnu libstdc++ 4.7版本并清理了标识符和格式以生成以下演示...
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
template<typename It>
bool next_permutation(It begin, It end)
{
if (begin == end)
return false;
It i = begin;
++i;
if (i == end)
return false;
i = end;
--i;
while (true)
{
It j = i;
--i;
if (*i < *j)
{
It k = end;
while (!(*i < *--k))
/* pass */;
iter_swap(i, k);
reverse(j, end);
return true;
}
if (i == begin) …Run Code Online (Sandbox Code Playgroud) 如何计算可变参数模板函数的参数数量?
即:
template<typename... T>
void f(const T&... t)
{
int n = number_of_args(t);
...
}
Run Code Online (Sandbox Code Playgroud)
number_of_args在上面实现的最佳方法是什么?
考虑以下程序:
#include <string>
#include <vector>
using namespace std;
struct T
{
int a;
double b;
string c;
};
vector<T> V;
int main()
{
V.emplace_back(42, 3.14, "foo");
}
Run Code Online (Sandbox Code Playgroud)
它不起作用:
$ g++ -std=gnu++11 ./test.cpp
In file included from /usr/include/c++/4.7/x86_64-linux-gnu/bits/c++allocator.h:34:0,
from /usr/include/c++/4.7/bits/allocator.h:48,
from /usr/include/c++/4.7/string:43,
from ./test.cpp:1:
/usr/include/c++/4.7/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = T; _Args = {int, double, const char (&)[4]}; _Tp = T]’:
/usr/include/c++/4.7/bits/alloc_traits.h:253:4: required from ‘static typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = …Run Code Online (Sandbox Code Playgroud) 在C++ 5.1.1/3 [expr.prim.general]中,它说:
类型和值类别[of
this]在静态成员函数中定义.
这是什么意思?它有什么关系?
注意:
this不应出现在静态成员函数的声明中
什么是摹立场在std::iostream的gcount,tellg和seekg会员?而p的pcount,tellp和seekp?
为什么他们不被称为count,tell而且seek?
G ++现在实现了C++ 11
thread_local关键字; 这与GNU__thread关键字的不同之处主要在于它允许动态初始化和销毁语义.不幸的是,这种支持需要对非函数局部thread_local变量的引用进行运行时惩罚, 即使它们不需要动态初始化,因此用户可能希望继续使用__thread具有静态初始化语义的TLS变量.
这种运行时惩罚的本质和来源究竟是什么?
显然,为了支持非函数局部thread_local变量,在进入每个线程main之前需要有一个线程初始化阶段(就像全局变量的静态初始化阶段一样),但是它们指的是一些超出该阶段的运行时惩罚?
粗略地说一下gcc的thread_local新实现的架构是什么?
如果我在命名空间栏中有一个类Foo:
namespace bar
{
class Foo { ... }
};
Run Code Online (Sandbox Code Playgroud)
那我可以:
using Baz = bar::Foo;
Run Code Online (Sandbox Code Playgroud)
现在就像我在名称空间中使用名称Baz定义类一样.
是否有可能为功能做同样的事情?
namespace bar
{
void f();
}
Run Code Online (Sandbox Code Playgroud)
然后:
using g = bar::f; // error: ‘f’ in namespace ‘bar’ does not name a type
Run Code Online (Sandbox Code Playgroud)
最干净的方法是什么?
解决方案还应该适用于模板功能.
定义:如果某个实体B是A 的别名,那么与源代码中的B替换A的任何或所有用法(当然不是声明或定义)相比,(剥离的)生成的代码保持不变.例如typedef A B是别名. #define B A是一个别名(至少). T& B = A不是别名,B可以有效地实现为间接指针,一个"unaliased"A可以使用"立即语义".
struct A {};
struct B : A {};
int main()
{
A* a = new B();
B* b = dynamic_cast<B*>(a);
}
Run Code Online (Sandbox Code Playgroud)
得到:
我如何制作A多态?我想安全地把它投到B.
(一种方法是添加一个虚拟虚函数,但是有更好的方法吗?)