所以我有一些类型X:
typedef ... X;
Run Code Online (Sandbox Code Playgroud)
和模板功能f:
class <typename T>
void f(X& x_out, const T& arg_in);
Run Code Online (Sandbox Code Playgroud)
然后是一个功能g:
void g(const X* x_array, size_t x_array_size);
Run Code Online (Sandbox Code Playgroud)
我需要编写一个variadic模板函数h来执行此操作:
template<typename... Args>
void h(Args... args)
{
constexpr size_t nargs = sizeof...(args); // get number of args
X x_array[nargs]; // create X array of that size
for (int i = 0; i < nargs; i++) // foreach arg
f(x_array[i], args[i]); // call f (doesn't work)
g(x_array, nargs); // call g with …Run Code Online (Sandbox Code Playgroud) 在最新的英特尔软件开发手册中,它描述了两个操作码前缀:
Group 2 > Branch Hints
0x2E: Branch Not Taken
0x3E: Branch Taken
Run Code Online (Sandbox Code Playgroud)
这些允许跳转指令的显式分支预测(像操作码一样Jxx)
我记得在几年前读过x86显式分支预测本质上是gccs分支谓词内在函数上下文中的无操作.
我现在还不清楚这些x86分支提示是否是一个新功能,或者它们在实践中是否基本上是无操作.
任何人都可以清除这个吗?
(那就是:gccs分支预测函数会生成这些x86分支提示吗? - 并且当前的Intel CPU不会忽略它们吗? - 这是什么时候发生的?)
更新:
我创建了一个快速测试程序:
int main(int argc, char** argv)
{
if (__builtin_expect(argc,0))
return 1;
if (__builtin_expect(argc == 2, 1))
return 2;
return 3;
}
Run Code Online (Sandbox Code Playgroud)
拆卸以下内容:
00000000004004cc <main>:
4004cc: 55 push %rbp
4004cd: 48 89 e5 mov %rsp,%rbp
4004d0: 89 7d fc mov %edi,-0x4(%rbp)
4004d3: 48 89 75 f0 mov %rsi,-0x10(%rbp)
4004d7: 8b 45 fc mov -0x4(%rbp),%eax
4004da: …Run Code Online (Sandbox Code Playgroud) C++ 11中的每个表达式都有一个值类别.lvalue,xvalue或prvalue之一.
有没有办法编写一个宏,给定任何表达式作为参数,将产生一个字符串"lvalue","xvalue"或"prvalue"酌情?
例如:
int main()
{
int x;
cout << VALUE_CAT(x) << endl; // prints lvalue
cout << VALUE_CAT(move(x)) << endl; // prints xvalue
cout << VALUE_CAT(42) << endl; // prints prvalue
}
Run Code Online (Sandbox Code Playgroud)
怎么可以VALUE_CAT实现?
我有一堆使用相同类型的make规则构建的应用程序:
apps = foo bar baz
all: $(apps)
foo: foo.o $(objects)
$(link)
bar: bar.o $(objects)
$(link)
baz: baz.o $(objects)
$(link)
Run Code Online (Sandbox Code Playgroud)
如果他们有扩展名(例如.x),我可以制定一个模式规则:
%.x: %.o $(objects)
$(link)
Run Code Online (Sandbox Code Playgroud)
我不必为每个应用程序写出新规则.
但他们没有扩展,我很确定:
%: %.o $(objects)
$(link)
Run Code Online (Sandbox Code Playgroud)
将无法工作(因为它指定构建任何文件,您可以使用此规则).
反正有没有指定一个涵盖所有$(apps)构建规则的规则?
在C++ 11标准的6.8.3中,它说:
如果在解析期间,模板参数中的名称的绑定方式与试验解析期间绑定的名称不同,则程序格式不正确.
由于此要求而导致程序错误的示例是什么?
在C++ 11中,以下函数声明:
int f(void);
Run Code Online (Sandbox Code Playgroud)
意思是:
int f();
Run Code Online (Sandbox Code Playgroud)
由非依赖类型void的单个未命名参数组成的参数列表等效于空参数列表.
我得到(也许是错误的)印象这是一个旧功能,也许是从C继承而来的?
有没有人知道这种方式背后的历史或理由来声明一个没有参数的函数?
我可以使用数组创建一个命名变量,如下所示:
char s[] = {1, 2, 3, 0};
if (strcmp(s, t))
...
Run Code Online (Sandbox Code Playgroud)
但是以下不起作用:
if (strcmp(char[]{1,2,3,0}, t))
...
Run Code Online (Sandbox Code Playgroud)
有没有办法用初始化列表指定临时的未命名数组?(在这种情况下,字符串文字可以工作,但对于char数组以外的数组?)
更新:
#include <iostream>
#include <cstring>
using namespace std;
typedef char CA[];
int main()
{
cout << CA{1,2,3, 0} << endl;
}
Run Code Online (Sandbox Code Playgroud)
给error: taking address of temporary array(g++-4.7.2 -std=gnu++11)
更新2:
我认为(也许)正在发生的事情是字符串文字被特别祝福为左值,但是临时数组是prvalues,因此你不能接受它们的地址.这是一个疯狂的猜测,我不确定.
更新3:
其实这应该是错的我想:
可以将"数组的NT"或"未知的T的数组"类型的左值或右值转换为"指向T的指针"的prvalue.结果是指向数组的第一个元素的指针.
以下C++ 11程序是否格式错误?
const int x[] = {1,2,3};
static_assert(x[0] == 1, "yay");
int main() {}
Run Code Online (Sandbox Code Playgroud)
gcc和clang似乎这么认为,但为什么不是x[0] == 1一个恒定的表达?
x[0] == 1
subscript operator
*(x+0) == 1
array-to-pointer conversion (int* p = x)
*(p+0) == 1
pointer addition
*p == 1
indirection (lvalue y = x[0])
y == 1
lvalue-to-rvalue conversion:
Run Code Online (Sandbox Code Playgroud)
一个非易失性glvalue(是的,x [0]是glvalue和非易失性)的整数(是的,它有类型const int)或枚举类型引用一个非易失性的const对象(是的它有类型const int)使用前面的初始化(是初始化为1),使用常量表达式初始化(是1是常量表达式)
似乎是真的,x数组的第一个元素满足这些条件.
1 == 1
Run Code Online (Sandbox Code Playgroud)
?
这是编译器错误,标准缺陷,还是我错过了什么?
5.19 [expr.const]的哪一部分说这不是一个常量表达式?
在最新的C++标准中,它意味着:
for (foo : bar)
baz;
Run Code Online (Sandbox Code Playgroud)
等同于:
{
auto && r = bar;
for ( auto it = r.begin(), end = r.end(); it != end; ++it )
{
foo = *it;
baz;
}
}
Run Code Online (Sandbox Code Playgroud)
当上面的bar是一个返回集合的函数调用时,例如:
vector<string> boo();
Run Code Online (Sandbox Code Playgroud)
即
for (auto bo : boo())
...
Run Code Online (Sandbox Code Playgroud)
这条线不会成为:
auto&& r = boo();
...
Run Code Online (Sandbox Code Playgroud)
因此boo()的临时返回值在语句"auto && r = boo()"的末尾被销毁,然后r是循环入口处的挂起引用.?? 这个推理是否正确?如果没有,为什么不呢?
如何在八度音程(或matlab)中比较两个结构的相等性?
试图使用==运算符产生:
binary operator `==' not implemented for `scalar struct' by `scalar struct' operations
Run Code Online (Sandbox Code Playgroud)