我刚刚读到了关于FastFormat C++ i/o格式库的文章,看起来好得令人难以理解:甚至比printf更快,类型安全,以及我认为令人满意的界面:
// prints: "This formats the remaining arguments based on their order - in this case we put 1 before zero, followed by 1 again"
fastformat::fmt(std::cout, "This formats the remaining arguments based on their order - in this case we put {1} before {0}, followed by {1} again", "zero", 1);
// prints: "This writes each argument in the order, so first zero followed by 1"
fastformat::write(std::cout, "This writes each argument in the order, so …Run Code Online (Sandbox Code Playgroud) 我只使用特定于C++的头文件(例如<cstdlib>),但是我仍然获得全局声明的函数,而不仅仅是std命名空间中的函数.有没有办法,也许是编译器开关,以防止这种情况?
例如,以下代码:
#include <cstdlib>
float random() { return 0.0f; }
int main() { return 0; }
Run Code Online (Sandbox Code Playgroud)
无法在linux下编译,出现以下错误:
> g++ -c main.cpp main.o
main.cpp: In function ‘float random()’:
main.cpp:2:14: error: new declaration ‘float random()’
/usr/include/stdlib.h:327:17: error: ambiguates old declaration ‘long int random()’
Run Code Online (Sandbox Code Playgroud)
要么
> clang++ main.cpp -o main.o
main.cpp:2:7: error: functions that differ only in their return type cannot be overloaded
float random() { return 0.0f; }
/usr/include/stdlib.h:327:17: note: previous declaration is here
extern long int random …Run Code Online (Sandbox Code Playgroud) C++提供了三种浮点类型:float,double和long double.我不经常在我的代码中使用浮点数,但是当我这样做时,我总是被类似无害线路上的警告所困扰
float PiForSquares = 4.0;
Run Code Online (Sandbox Code Playgroud)
问题是文字4.0是双重的,而不是浮动的 - 这很刺激.
对于整数类型,我们有short int,int和long int,这非常简单.为什么C不具有短浮动,浮动和长浮动?在地球上"双重"来自哪里?
编辑:浮动类型之间的关系似乎与整数类似.double必须至少与float一样大,而long double至少与double一样大.没有其他精度/范围的保证.
来自C背景,我一直认为POD类型(例如整数)从未在C++中自动进行零初始化,但似乎这是完全错误的!
我的理解是,只有'裸'非静态POD值不会被填零,如代码片段所示.我做对了,还有其他重要的案例我错过了吗?
static int a;
struct Foo { int a;};
void test()
{
int b;
Foo f;
int *c = new(int);
std::vector<int> d(1);
// At this point...
// a is zero
// f.a is zero
// *c is zero
// d[0] is zero
// ... BUT ... b is undefined
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
在C++中通过值传递或通过常量引用传递是否更好?
我知道在C++中传递值,指针和引用的区别,我会考虑在C++中按值(而不是const引用)传递对象几乎总是编程错误.
void foo(Obj o); ... // Bad
void foo(const Obj &o); ... // Better
Run Code Online (Sandbox Code Playgroud)
唯一可以考虑通过值而不是const引用传递的地方的唯一情况是对象小于引用的位置,因此传递值更有效.
但是,这肯定是构建编译器以确定的那种东西吗?
为什么C++实际上需要传递值并通过const引用传递,并且 - 编译器是否允许自动将调用转换为(和来自)const引用(如果适用)?
(似乎有100个C++调用约定问题,询问(比如)值和引用之间的差异 - 但我找不到一个问"为什么?".)
我想从不同长度的变量中提取最后两个字段值.例如,考虑以下三个值:
fe80::e590:1001:7d11:1c7e
ff02::1:ff1f:fb6
fe80::7cbe:e61:f5ab:e62 ff02::1:ff1f:fb6
Run Code Online (Sandbox Code Playgroud)
这三条线的长度可变.如果我用分隔符分割每一行,我想只提取最后两个字段值:
也就是说,从三行开始,我想:
7d11, 1c7e
ff1f, fb6
ff1f, fb6
Run Code Online (Sandbox Code Playgroud)
这可以用split()吗?我没有得到任何想法.
Android NinePatch图像似乎是带有额外信息的标准.png文件.是否有任何格式的规范,因为我希望能够在其他平台上实现这一点?
我想知道为什么我们使用术语"推"和"弹出"来添加/删除堆栈中的项目?是否有一些物理比喻导致这些术语变得普遍?
我唯一的建议就像弹簧式手枪杂志一样,轮子被"推"进去,可以"弹出",但这似乎有点不太可能.
第二个堆栈琐事问题:为什么大多数CPU实现调用堆栈在内存中向下增长而不是向上?
我有一个带有多个面板的表单,每个面板都有Align = alTop,因此它们从表单的顶部很好地堆叠.
但是,我想动态更改这些面板的外观顺序 - 即上下移动它们.这样做的最佳方法是什么?
我有一个自动重置事件对象,如下所示:
handle = CreateEvent(NULL, true, false, NULL);
Run Code Online (Sandbox Code Playgroud)
...(和某些单元测试目的)我想检查它是否在某一点发出信号.我知道使用事件的"正确"方式 - 这纯粹是为了诊断工具.
对于手动重置事件,我可以使用......
bool signalled = WaitForSingleObjectEx(handle, 0, true) != WAIT_TIMEOUT;
Run Code Online (Sandbox Code Playgroud)
...但是对于具有重置它们的副作用的自动重置事件.我想我可以试试这个,但我觉得应该有一个不那么危险的方式......?
bool isSignalled(HANDLE handle)
{
bool signalled = WaitForSingleObjectEx(handle, 0, true) != WAIT_TIMEOUT;
// warning - event is now reset. Maybe need to wrap this in a critical section or similar?
if (signalled)
SetEvent(handle);
return signalled;
}
Run Code Online (Sandbox Code Playgroud) c++ ×6
alignment ×1
android ×1
c ×1
clang++ ×1
constructor ×1
delphi ×1
events ×1
fastformat ×1
file-format ×1
g++ ×1
history ×1
ieee-754 ×1
nine-patch ×1
python ×1
stack ×1
tpanel ×1
winapi ×1