请考虑以下代码:
#include <iostream>
#include <typeinfo>
#include <type_traits>
using namespace std;
struct A { int data; };
struct B1 : A {};
struct B2 : virtual A {};
struct Base1 : virtual A {};
struct Base2 : virtual A {};
struct Derived : Base1, Base2 {};
int main() {
cout << sizeof(B1) << endl;
cout << sizeof(B2) << endl;
cout << sizeof(Derived) << endl;
cout << std::is_polymorphic<B1>::value << endl;
cout << std::is_polymorphic<B2>::value << endl;
cout << std::is_polymorphic<Derived>::value << endl;
return 0; …Run Code Online (Sandbox Code Playgroud) 我正在研究一些在 Windows 上运行的用 Delphi 7 编写的遗留软件。我已将问题缩小到以下程序:
var f: text;
begin
assign(f, 'a.txt');
rewrite(f);
writeln(f, 'before' + chr(14) + 'after');
close(f);
assign(f, 'a.txt');
append(f);
close(f);
end.
Run Code Online (Sandbox Code Playgroud)
我希望它创建a.txt包含的文件"before#14after#13#10",然后不附加任何内容。但是,在我在 Windows 上运行这个程序后,我看到的是beforein a.txt,就像 Delphi 的append截断文件一样。如果我不重新打开文件,它会before#14after#13#10按预期显示。
如果我FooBar在重新打开的文件中写了一些 ( ),它会被附加,但好像文件已经被截断了:beforeFooBar.
这种效果不会发生在 0 到 32 之间的任何其他字符上,即使是 26(代表 EOF)。
这是 Delphi 中的错误还是定义明确的行为?有什么特别之处chr(14)?
我有以下代码:
#include <cstdio>
#include <iostream>
using std::cout;
struct SomeType {
SomeType() {}
SomeType(const SomeType &&other) {
cout << "SomeType(SomeType&&)\n";
*this = std::move(other);
}
void operator=(const SomeType &) {
cout << "operator=(const SomeType&)\n";
}
void operator=(SomeType &&) {
cout << "operator=(SomeType&&)\n";
}
};
int main() {
SomeType a;
SomeType b(std::move(a));
b = std::move(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望移动构造函数调用移动赋值运算符.这是该程序的输出:
SomeType(SomeType&&)
operator=(const SomeType&)
operator=(SomeType&&)
Run Code Online (Sandbox Code Playgroud)
如您所见,移动赋值运算符已成功调用,但在分配给*this内部移动构造函数时则不会.为什么会这样,我能以某种方式修复它吗?
我知道你永远不应该使用std::find(some_map.begin(), some_map.end())或std::lower_bound,因为它需要线性时间而不是由对数提供的对数some_map.lower_bound.类似的事情发生在std::list:有std::list::sort排序功能,但你无法调用std::sort(some_list.begin(), some_list.end()),因为迭代器不是随机访问.
但是,std::swap例如,标准容器有重载,因此调用swap(some_map, other_map)需要O(1),而不是O(n).为什么不C++标准给我们的专门版本,lower_bound以及find地图和套?有深层原因吗?
我正在研究一个长整数库作为我的 C++ 家庭作业,我们的老师提供了它的接口使用示例。这是该文件的一部分:
void test_conversions()
{
int ordinary = 42;
lint long_int(ordinary);
long_int = ordinary;
ordinary = static_cast<int>(long_int); // ok
std::string s("-15");
lint z(s);
z.to_string() == s; // this should be true
}
void failed_conversions_test()
{
int ordinary = 42;
std::string str = "-42";
lint x = 5;
ordinary = x; // must be compilation error!
x = str; // must be compilation error!
str = x; // must be compilation error!
}
Run Code Online (Sandbox Code Playgroud)
我想在构建过程中测试这个文件的兼容性。如您所见,应该有四个测试:一个用于编译成功 ( test_conversions),三个用于failed_conversions_test. 我可以很容易地通过添加存根实现汇编检查成功 …
每个人都知道sqrt,从功能math.h/ cmath在C/C++ -它返回它的参数的平方根.当然,它必须有一些错误,因为不是每个数字都可以精确存储.但我保证结果有一定的精确度吗?例如,'它是可以用浮点类型表示的平方根的最佳近似值,or如果你计算结果的平方,它将尽可能接近初始参数使用给定的浮点类型?
C/C++标准有什么关于它的吗?
我可以通过手动编写比较器来比较两个自然数:
is-? : ? ? ? ? Bool
is-? zero _ = true
is-? (suc _) zero = false
is-? (suc x) (suc y) = is-? x y
Run Code Online (Sandbox Code Playgroud)
但是,我希望标准库中有类似的东西,所以我不会每次都写。
我可以在Data.Nat中找到_?_运算符,但这是一个参数化类型,基本上包含一个“证明”,即一个特定数字小于另一个(类似于)。有没有办法使用它或其他方式来理解哪个数字小于另一个“在运行时”的数字(例如,返回)?_?_Bool
我要解决的更大问题:
readNat : List Char ? Maybe (? × List Char)函数。它尝试从列表的开头读取自然数;稍后将成为的一部分sscanf。digit : Char ? Maybe ?将解析单个十进制数字的辅助函数。primCharToNat c用primCharToNat '0',primCharToNat '1'并决定是否返回None或(primCharToNat c) ? (primCharToNat '0')考虑以下示例:
struct S {
template<typename T = void>
friend void foo(S) {
}
};
int main() {
S s;
foo(s); // (1)
foo<void>(s); // (2)
}
Run Code Online (Sandbox Code Playgroud)
我的 GCC 9.2.0 无法编译(2)并出现以下错误:
a.cpp: In function 'int main()':
a.cpp:10:5: error: 'foo' was not declared in this scope
10 | foo<void>(s);
| ^~~
a.cpp:10:9: error: expected primary-expression before 'void'
10 | foo<void>(s);
| ^~~~
Run Code Online (Sandbox Code Playgroud)
不过,(1)效果很好。为什么是这样?如何foo使用显式模板参数进行调用?
在 C++ 中,存在臭名昭著的自赋值问题:在实现 时operator=(const T &other),必须小心,this == &other不要this在从 复制数据之前破坏 的数据other。
然而,*this和other可能以比作为同一个对象更有趣的方式交互。即,一个可以包含另一个。考虑以下代码:
#include <iostream>
#include <string>
#include <utility>
#include <vector>
struct Foo {
std::string s = "hello world very long string";
std::vector<Foo> children;
};
int main() {
std::vector<Foo> f(4);
f[0].children.resize(2);
f = f[0].children; // (1)
// auto tmp = f[0].children; f = std::move(tmp); // (2)
std::cout << f.size() << "\n";
}
Run Code Online (Sandbox Code Playgroud)
我希望行(1)和(2)是相同的:程序被明确定义为 print 2。然而,我还没有找到一个编译器+标准库组合,可以与启用的行 …
c++ assignment-operator language-lawyer copy-assignment memory-aliasing
我现在正在发现Meteor DDP协议,没有太多关于它的文档.
我发现的是Meteor服务器在每个消息块前面发送一个字符(这些消息像字符串一样发送,为什么?你知道吗?),如下所示:
c[2010,"Another connection still open"]
o
a["{\"server_id\":\"0\"}","{\"msg\":\"connected\",\"session\":\"BFWEff4389fjHFure\"}"]
a["{\"msg\":\"ready\",\"subs\":[\"fefjuihYFrvnuKOEF\"]}"]
Run Code Online (Sandbox Code Playgroud)
(钥匙被更换了)
预先添加这个角色的目的是什么?我在哪里可以阅读更多有关它和低级别DDP规范的内容?我保证(至少对于pre1版本)所有消息都以字符串编码,并且这些字符串连接成数组,并且每个这样的数组都以自定义字符为前缀?
我正在探索 Meteor 的平衡选项。这篇文章看起来很酷,它说应该支持以下内容来负载平衡 Meteor:
据我了解,“粘性会话支持”是指在会话期间将一个客户端分配给同一台服务器。这是必要的吗?如果我根本不配置粘性会话,可能会发生什么?
这是我自己想到的:
会发生什么坏事?这三点看起来并不是很糟糕:数据有效、可用,可能是以额外的内存消耗为代价的。
请考虑以下代码:
#include <cstdio>
struct A {
A() {
printf("Bar\n");
}
};
int main() {
printf("Foo\n");
A a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是否保证Foo\nBar\n按顺序打印?我的经验说"是",但是我想从C++ 11标准或MSDN引用中引用一些引用,因为有人告诉我编译器可能在实际的声明行之前调用构造函数.
如果构造函数有一些参数(因为它们可以依赖于函数启动时未计算的值),那将更加明显,如果只有默认构造函数则不太明显.比方说,JavaScript以在var可用行之前定义变量而闻名:
function main() {
console.log(x);
var x = 2;
console.log(y);
}
main();
Run Code Online (Sandbox Code Playgroud)
上面的代码将打印undefined为值,x然后失败y is not defined.
考虑以下代码main.cpp:
#include <iostream>
int main() {
std::cout << "Hello World!\n";
}
Run Code Online (Sandbox Code Playgroud)
编译g++ main.cpp -o -main失败:
/usr/bin/ld: unrecognised emulation mode: ain
Supported emulations: elf_x86_64 elf32_x86_64 elf_i386 elf_iamcu elf_l1om elf_k1om i386pep i386pe
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我使用的是在 WSL2 中运行的 64 位 Ubuntu 20.04.3 LTS。GCC 的版本是
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Run Code Online (Sandbox Code Playgroud)
如何编译这个 Hello World?