假设我有一些constexpr函数f:
constexpr int f(int x) { ... }
Run Code Online (Sandbox Code Playgroud)
我在编译时知道一些const int N:
或
#define N ...;
Run Code Online (Sandbox Code Playgroud)
要么
const int N = ...;
Run Code Online (Sandbox Code Playgroud)
根据你的答案需要.
我想要一个int数组X:
int X[N] = { f(0), f(1), f(2), ..., f(N-1) }
Run Code Online (Sandbox Code Playgroud)
这样在编译时评估函数,X中的条目由编译器计算,结果放在我的应用程序映像的静态区域,就像我在X初始化列表中使用整数文字一样.
有什么方法可以写这个吗?(例如,使用模板或宏等)
我有最好的:(感谢Flexo)
#include <iostream>
#include <array>
using namespace std;
constexpr int N = 10;
constexpr int f(int x) { return x*2; }
typedef array<int, N> A;
template<int... i> constexpr A fs() { return A{{ f(i)... }}; }
template<int...> struct S;
template<int... i> struct S<0,i...>
{ static …Run Code Online (Sandbox Code Playgroud) 可能重复:
git push后本地执行挂钩?
git有许多钩子,可以在某些事件附近调用脚本.
是否可以在我的工作存储库中设置一个git hook,在我推送到远程分支后在本地执行某些操作?如果是这样的钩子呢?
我有一个程序包含一个处理阶段,需要从多态类型树中使用一堆不同的对象实例(都在堆上分配),所有这些实例最终都是从一个公共基类派生出来的.
由于实例可能循环引用彼此,并且没有明确的所有者,我希望将它们分配new,用原始指针处理它们,并将它们留在内存中用于阶段(即使它们变得未被引用),然后在阶段之后使用这些实例的程序,我想一次删除它们.
我如何构建它如下:
struct B; // common base class
vector<unique_ptr<B>> memory_pool;
struct B
{
B() { memory_pool.emplace_back(this); }
virtual ~B() {}
};
struct D : B { ... }
int main()
{
...
// phase begins
D* p = new D(...);
...
// phase ends
memory_pool.clear();
// all B instances are deleted, and pointers invalidated
...
}
Run Code Online (Sandbox Code Playgroud)
除了小心所有B实例都使用new分配,并且在清除内存池后没有人使用任何指针时,这个实现是否存在问题?
具体来说,我担心在派生类构造函数完成之前,this指针用于std::unique_ptr在基类构造函数中构造a .这是否会导致未定义的行为?如果有的话有解决方法吗?
根据C++ 1y/C++ 14 N3690,变量模板特化的类型是否必须与主模板的类型相同?
template<int x>
char y = f(x);
template<>
double y<42> = g();
Run Code Online (Sandbox Code Playgroud)
如果是这样,是否有可能以某种方式保留主要的未定义?
template<int x>
???? y = ???; // undefined
template<>
double y<42> = g();
Run Code Online (Sandbox Code Playgroud)
草案涵盖哪些内容?
类模板的等效功能是:
template<int x>
struct S
{
static char y;
};
template<>
struct S<42>
{
static double y;
};
Run Code Online (Sandbox Code Playgroud)
和
template<int x>
struct S; // undefined
template<>
struct S<42>
{
static double y;
};
Run Code Online (Sandbox Code Playgroud) 假设我有一个模板功能:
template<typename T>
void f(T t)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我想为所有原始整数类型编写一个专门化.做这个的最好方式是什么?
我的意思是:
template<typename I where is_integral<I>::value is true>
void f(I i)
{
...
}
Run Code Online (Sandbox Code Playgroud)
并且编译器为整数类型选择第二个版本,为其他所有类型选择第一个版本?
我有两个git分支,"A"和"B",并提交编号为1到8.我的历史看起来像这样
1 -> 2 -> 3 -> 4[A] -> 5 -> 6 -> 7 -> 8[B]
Run Code Online (Sandbox Code Playgroud)
我想改变它,所以我的历史看起来像这样:
1 -> 2 -> 3 -> 4 -> 5 -> 6[A] -> 7 -> 8[B]
Run Code Online (Sandbox Code Playgroud)
也就是说,我想将分支A的头部从提交4移动到提交6.
我用什么命令来做这个?
请考虑以下代码:
unordered_set<T> S = ...;
for (const auto& x : S)
if (...)
S.insert(...);
Run Code Online (Sandbox Code Playgroud)
这打破了吗?如果我们在S中插入一些东西,那么迭代器可能会失效(由于重新散列),这将打破范围 - 因为它在使用S.begin ... S.end.
有一些模式可以解决这个问题吗?
一种方法是:
unordered_set<T> S = ...;
vector<T> S2;
for (const auto& x : S)
if (...)
S2.emplace_back(...);
for (auto& x : S2)
S.insert(move(x));
Run Code Online (Sandbox Code Playgroud)
这看起来很笨重.有没有更好的方法让我失踪?
(特别是如果我使用的是手动哈希表,并且我可以阻止它重新散列直到循环结束,那么使用第一个版本是安全的.)
更新:
来自http://en.cppreference.com/w/cpp/container/unordered_map/insert
如果由于插入而发生重新散列,则所有迭代器都将失效.否则迭代器不会受到影响.引用不会失效.仅当新元素数高于时,才会发生重新散列
max_load_factor() * bucket_count().
你能以max_load_factor某种方式搞乱以防止重复吗?
假设我有一个类型模板参数T.
假设我有std::aligned_storage以下内容:
typename std::aligned_storage<sizeof(T), alignof(T)>::type storage;
Run Code Online (Sandbox Code Playgroud)
我想把新的T放入storage.
什么是传递给贴片新运算符的符合标准的指针值/类型,以及如何从中派生出来storage?
new (& ???) T(a,b,c);
Run Code Online (Sandbox Code Playgroud)
例如:
new (&storage) T(a,b,c);
new (static_cast<void*>(&storage)) T(a,b,c);
new (reinterpret_cast<T*>(&storage)) T(a,b,c);
new (static_cast<T*>(static_cast<void*>(&storage));
Run Code Online (Sandbox Code Playgroud)
以上哪些(如果有的话)是合规的,如果没有,那么更好的方法是什么?
假设我要编写一个调用null函数100次的函数。这些实现中哪一个最好,为什么?
template<typename F>
void call100(F f) {
for (int i = 0; i < 100; i++)
f();
}
template<typename F>
void call100(F& f) {
for (int i = 0; i < 100; i++)
f();
}
template<typename F>
void call100(const F& f) {
for (int i = 0; i < 100; i++)
f();
}
template<typename F>
void call100(F&& f) {
for (int i = 0; i < 100; i++)
f();
}
Run Code Online (Sandbox Code Playgroud)
还是有更好的实施方案?
关于4的更新
struct S {
S() {}
S(const S&) = …Run Code Online (Sandbox Code Playgroud) 我的HTML中有一个链接:
<a href="/DoSomethingDangerous">do something dangerous</a>
Run Code Online (Sandbox Code Playgroud)
访问DoSomethingDangerous链接会导致不容易发生可逆操作.
因此,在点击链接后,我想要一个对话框(例如"你确定吗?""确定""取消"),如果用户点击取消,则不访问链接,浏览器仍然在同一页面.
使用Javascript或jQuery实现这个的最干净的技术是什么?