在嵌套结构中有灵活的数组成员是有效的C代码吗?那么下面的示例代码是否可以通过合理的编译器按预期工作?
#include <stdio.h>
#include <stdlib.h>
struct d {
char c;
int ns[];
};
struct c {
struct d d;
};
struct b {
struct c c;
};
struct a {
int n;
struct b b;
};
int main() {
const int n = 10;
struct a *pa = malloc(sizeof(*pa) + n * sizeof(pa->b.c.d.ns[0]));
pa->n = n;
pa->b.c.d.c = 1;
for (int i = 0; i < n; ++i) {
pa->b.c.d.ns[i] = i;
}
for (int i = 0; i < …Run Code Online (Sandbox Code Playgroud) int main() {
double inf = INFINITY;
double pi = acos(-1.0);
printf("[1]: %f %f\n", atan(inf) / pi, atan(-inf) / pi);
printf("[2]: %f %f\n", tan(inf) / pi, tan(-inf) / pi);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
产出
[1]: 0.500000 -0.500000
[2]: -nan -nan
Run Code Online (Sandbox Code Playgroud)
标准是否定义了这种行为?是[2]未定义的行为吗?未指定?
我想确定至少[1]是一个有保证的结果。
任何人都可以解释为什么这是一个语法错误?
f =
f'
where f' = do
if True then
return ()
else
return ()
main = f
Run Code Online (Sandbox Code Playgroud)
如果我给if块更多缩进,那么它会以某种方式编译好.
f =
f'
where f' = do
if True then
return ()
else
return ()
main = f
Run Code Online (Sandbox Code Playgroud)
或者我可以分开where,我通常会这样做.
f =
f'
where
f' = do
if True then
return ()
else
return ()
main = f
Run Code Online (Sandbox Code Playgroud)
我开始赏金以获得下面两个问题的好解释.(是的,我读了Haskell报告.因为不理解10.3布局而感到羞耻)
似乎我需要明确说明forall在数据定义中有一个参数类型.例如,这个
data A = A (forall s. ST s (STUArray s Int Int))
Run Code Online (Sandbox Code Playgroud)
这将是有效的
data A = A (ST s (STUArray s Int Int))
Run Code Online (Sandbox Code Playgroud)
惯于.
也许我问的是一些太明显的东西,但我不明白这个原因,因为在大多数其他情况下你不需要明确forall指定一个参数类型; 编译器会这样做.那么这里的区别是什么?
通过以下程序,
f 0 0 0 1 = 0
f 0 0 1 0 = f 0 0 0 1 + 1
f 0 1 0 0 = f 0 0 1 1 + 1
f 1 0 0 0 = f 0 1 1 1 + 1
f a b c d = (p + q + r + s) / (a + b + c + d)
where
p
| a > 0 = a * f (a - …Run Code Online (Sandbox Code Playgroud) 预计在"hello world"之后5秒,"hi hi"会出现在控制台上.但实际上,控制台是空的5秒钟,然后我可以看到这两个消息.我该怎么做才能获得预期的结果?
#include <QThread>
#include <QTextStream>
QTextStream qout(stdout);
int main()
{
qout << "hello world\n";
QThread::sleep(5);
qout << "hi again\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
//"多线程"标记就在那里,因为我在编写多线程程序时发现了这个问题,并且该解决方案可以应用于多线程应用程序.
在下面的(伪)代码中,cond无论出于何种原因,它可能会在不应该唤醒的情况下唤醒.所以我在那里放了一个while循环.当它确实唤醒时,它仍将消耗锁定,因此可以保证out()只有一个线程正在执行其工作.
但是,如果虽然有一个虚假的唤醒out(),同时in()发出信号out(),但是在那个时刻out()由于虚假的唤醒已经被锁定,会发生什么.那么如果cond向锁定线程发出信号会发生什么?
in()
inLock.lock()
isEmpty = false
cond.signal()
inLock.unlock()
out()
outLock.lock()
while isEmpty
cond.wait(outLock)
isEmpty = true
outLock.unlock()
Run Code Online (Sandbox Code Playgroud)
注意
那么,是100%的安全,我知道我可以使用一个互斥两个in()和out(),但我使用的数据结构是100%安全的,当输入和输出发生在同一时间; 它是一种队列.而且我认为在填充一些新数据时阻止从队列读出的任何内容都是一种性能折衷,反之亦然.
我确实考虑过使用信号量,但问题是,无论出于何种原因,许多C和C++库都没有实现信号量.
uint32_t Seed() {
uint64_t seed = GetSomeReasonable64BitIntegerSeed();
return *(uint32_t*)&seed ^ *((uint32_t*)&seed + 1);
}
Run Code Online (Sandbox Code Playgroud)
上面不是真正的代码,但这基本上是真正的代码所做的.我从g ++那里得到一个警告,它违反了严格的别名,谷歌搜索它,好吧我想解决它.我发现了这个问题,但它没有提供一个明确的解决方案,除了使用memcpy或依赖于未定义但实际上没有问题的行为,即访问联合的未设置成员.
我能想到的当前选择是,
memcpy.union并将此部分编译为C,其中语言标准允许通过联合进行类型惩罚.#include <iostream>
#include <vector>
struct S {
//std::vector<int> ns(1); //ERROR!
std::vector<int> ns = std::vector<int>(1);
};
int main() {
S s;
std::cout << (s.ns[0] = 123) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用括号初始值设定项似乎是一个错误.这背后的目的是什么?
这个
int main()
{
std::cout << range(1, 11).reverse().sort().sum() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
就是所有内容main,正如代码所说,它创建了一个从1到10(包括1和10)的列表,将其反转,通过排序取消反转,并计算总和.因此输出应为55.
代码是一个实验,(ab)使用constexprC++ 14中的宽松要求.我尽力创建一个编译时列表类,但实际上还远远不够.这个类是不完整的,但它仍然可以模仿很多功能风格的编程.
据我所知,需要constexpr让编译器在编译时评估一些东西.所以我认为编译器可以简单地用代码55替换所有代码,但事实并非如此.代码确实具有在编译时获得结果所需的一切.我错过了什么?
从评论中,我试图通过使用结果来检查问题static_assert.clang和gcc都给了我一个错误,但我也没理解,而后者似乎被打破了......
铛
a.cpp:142:17: error: static_assert expression is not an integral constant expression
static_assert(range(1, 11).reverse().sort().sum() == 55, "");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cpp:60:23: note: assignment to object outside its lifetime is not allowed in a constant
expression
l.array[l.length++] = t;
^
a.cpp:134:9: note: in call to '&l->add(1)'
l = l.add(a);
^
a.cpp:142:17: note: in call to 'range(1, 11, 1)'
static_assert(range(1, 11).reverse().sort().sum() …Run Code Online (Sandbox Code Playgroud)