小编yep*_*ons的帖子

C++:是一个具有虚拟基础的类但没有虚函数的多态并且具有VTable?

请考虑以下代码:

#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)

c++ polymorphism virtual-functions rtti virtual-inheritance

6
推荐指数
1
解决办法
939
查看次数

为什么 Delphi 7 在追加模式下打开时会在 ASCII 码 14 之后截断文件?

我正在研究一些在 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)

delphi ascii

6
推荐指数
1
解决办法
188
查看次数

C++移动语义:为什么调用复制赋值operator =(&)而不是移动赋值operator =(&&)?

我有以下代码:

#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内部移动构造函数时则不会.为什么会这样,我能以某种方式修复它吗?

c++ move operator-overloading move-constructor c++11

5
推荐指数
2
解决办法
1495
查看次数

为什么std :: map没有find/lower_bound的重载,std :: list没有sort的重载?

我知道你永远不应该使用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++ set lower-bound template-specialization

5
推荐指数
1
解决办法
597
查看次数

如何在应该失败的 CMake 中实现编译测试?

我正在研究一个长整数库作​​为我的 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. 我可以很容易地通过添加存根实现汇编检查成功 …

c++ testing compiler-errors compilation cmake

4
推荐指数
1
解决办法
1306
查看次数

C/C++中sqrt函数的保证精度

每个人都知道sqrt,从功能math.h/ cmath在C/C++ -它返回它的参数的平方根.当然,它必须有一些错误,因为不是每个数字都可以精确存储.但我保证结果有一定的精确度吗?例如,'它是可以用浮点类型表示的平方根的最佳近似值,or如果你计算结果的平方,它将尽可能接近初始参数使用给定的浮点类型?

C/C++标准有什么关于它的吗?

c precision math.h floating-accuracy sqrt

4
推荐指数
1
解决办法
4464
查看次数

如何用标准库(如N-&gt; N-&gt; Bool)比较Agda中的两个自然数?

我可以通过手动编写比较器来比较两个自然数:

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

我要解决的更大问题:

  1. 作为分配的一部分,我正在编写一个readNat : List Char ? Maybe (? × List Char)函数。它尝试从列表的开头读取自然数;稍后将成为的一部分sscanf
  2. 我想实现digit : Char ? Maybe ?将解析单个十进制数字的辅助函数。
  3. 为了做到这一点我想比较primCharToNat cprimCharToNat '0'primCharToNat '1'并决定是否返回None(primCharToNat c) ? (primCharToNat '0')

agda comparison-operators

4
推荐指数
1
解决办法
527
查看次数

为什么我不能使用显式模板参数调用模板友元函数?

考虑以下示例:

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++ templates friend friend-function

4
推荐指数
1
解决办法
8088
查看次数

为什么将容器的元素分配给容器(而不是)定义良好的 C++?

在 C++ 中,存在臭名昭著的自赋值问题:在实现 时operator=(const T &other),必须小心,this == &other不要this在从 复制数据之前破坏 的数据other

然而,*thisother可能以比作为同一个对象更有趣的方式交互。即,一个可以包含另一个。考虑以下代码:

#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

4
推荐指数
1
解决办法
129
查看次数

Meteor DDP服务器到客户端规范:开头的奇怪字符

我现在正在发现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版本)所有消息都以字符串编码,并且这些字符串连接成数组,并且每个这样的数组都以自定义字符为前缀?

communication meteor ddp

3
推荐指数
1
解决办法
449
查看次数

SockJS 和meteor:如果负载均衡器不支持粘性会话怎么办?

我正在探索 Meteor 的平衡选项。这篇文章看起来很酷,它说应该支持以下内容来负载平衡 Meteor:

  1. 蒙戈·奥普泰林。否则,一个 Meteor 实例可能需要长达 10 秒的时间才能从另一个实例获取更新,因为将使用轮询 Mongo 驱动程序,该驱动程序每 10 秒轮询并比较一次数据库。
  2. 网络套接字。这也很清楚 - 否则客户端将回退到 HTTP 和长轮询,这会起作用,但它不如 Websocket 那么酷。
  3. 粘性会话“SockJS 需要”。问题来了:

据我了解,“粘性会话支持”是指在会话期间将一个客户端分配给同一台服务器。这是必要的吗?如果我根本不配置粘性会话,可能会发生什么?

这是我自己想到的:

  1. 因为 Meteor 将所有发送给客户端的数据存储在内存中,如果客户端连接到 X 个服务器,那么将消耗 X 倍的内存
  2. 对于同一用户,在不同的选项卡或窗口中可能会出现一些较小的(或主要的,如果没有 oplog)延迟,这可能会令人惊讶。
  3. 如果 SockJS 重新连接并希望一些数据在重新连接后仍保留,那么情况会很糟糕。我不确定 SockJS 是如何工作的,这一点有效吗?

会发生什么坏事?这三点看起来并不是很糟糕:数据有效、可用,可能是以额外的内存消耗为代价的。

load-balancing meteor sockjs

3
推荐指数
1
解决办法
2111
查看次数

是否保证在定义变量时确切地调用构造函数?

请考虑以下代码:

#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.

c++ standards constructor undefined-behavior c++11

2
推荐指数
3
解决办法
118
查看次数

在 Ubuntu 上使用 gcc 编译时出现“无法识别的模拟模式:ain”

考虑以下代码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?

c++

1
推荐指数
1
解决办法
1081
查看次数