在我们所有的C++课程,所有的老师总是把using namespace std;
在之后#include
在它们的S .h
文件.从那时起,我觉得这很危险,因为在另一个程序中包含该标题我会将命名空间导入到我的程序中,可能没有意识到,打算或想要它(标题包含可以非常深入嵌套).
所以我的问题是双重的:我是对的,using namespace
不应该在头文件中使用,和/或是否有某种方法来撤消它,例如:
//header.h
using namespace std {
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
还有一个问题是:标题文件是否应该是#include
相应.cpp
文件所需的所有标题,只有那些标题定义所需的标题,并让.cpp
文件#include
为其余标题,或者没有,并声明它需要的所有内容extern
?
问题背后的原因与上面相同:在包含.h
文件时我不想要惊喜.
另外,如果我是对的,这是一个常见的错误吗?我的意思是在现实世界的编程和"真正的"项目中.
谢谢.
我们有一个函数,它使用了自身内部的非捕获 lambda,例如:
void foo() {
auto bar = [](int a, int b){ return a + b; }
// code using bar(x,y) a bunch of times
}
Run Code Online (Sandbox Code Playgroud)
现在 lambda 实现的功能在其他地方变得需要,所以我将把 lambda 提升foo()
到全局/命名空间范围之外。我可以将其保留为 lambda,使其成为复制粘贴选项,或者将其更改为适当的函数:
auto bar = [](int a, int b){ return a + b; } // option 1
int bar(int a, int b){ return a + b; } // option 2
void foo() {
// code using bar(x,y) a bunch of times
}
Run Code Online (Sandbox Code Playgroud)
将其更改为适当的函数是微不足道的,但这让我想知道是否有理由不将其保留为 lambda?有什么理由不只是到处使用 lambda 而不是“常规”全局函数?
如果我有一个struct
C++,有没有办法安全地读/写一个跨平台/编译器兼容的文件?
因为如果我理解正确,每个编译器"填充"基于目标平台不同.
在C/C++中,如果我有以下功能:
void foo();
void bar(void (*funcPtr)());
Run Code Online (Sandbox Code Playgroud)
这两个电话之间有区别吗:
bar(foo);
bar(&foo);
Run Code Online (Sandbox Code Playgroud)
?
以下程序的输出似乎自相矛盾:
#include <type_traits>
#include <iostream>
#include <functional>
void foo(int&){ std::cout << "called\n"; }
int main() {
int a;
foo(a);
std::cout << std::is_invocable_v<decltype(foo), decltype(a)> << std::endl;
std::invoke(foo, a);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
called
0
called
Run Code Online (Sandbox Code Playgroud)
在我看来,调用一个不可调用的函数?这里发生了什么?
我总是理解在C中,func
并且&func
是等价的.我假设它们都应该是类型指针,在我的Win64系统上是8个字节.但是,我刚试过这个:
#include <stdio.h>
int func(int x, int y)
{
printf("hello\n");
}
int main()
{
printf("%d, %d\n", sizeof(&func), sizeof(func));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
而期望获得输出8, 8
却很惊讶8, 1
.
为什么是这样?到底是 什么类型的func
?它似乎是类型char
或某种等价物.
这里发生了什么?
gcc -std=c99
如果它有所作为,我编译了这个.
以下似乎无论如何都有效.使用的优点(除了好的repr
)有什么用types.SimpleNamespace
?或者它是一回事吗?
>>> import types
>>> class Cls():
... pass
...
>>> foo = types.SimpleNamespace() # or foo = Cls()
>>> foo.bar = 42
>>> foo.bar
42
>>> del foo.bar
>>> foo.bar
AttributeError: 'types.SimpleNamespace' object has no attribute 'bar'
Run Code Online (Sandbox Code Playgroud) 在c/c ++中(我假设它们在这方面是相同的),如果我有以下内容:
struct S {
T a;
.
.
.
} s;
Run Code Online (Sandbox Code Playgroud)
以下是否保证是真的?
(void*)&s == (void*)&s.a;
Run Code Online (Sandbox Code Playgroud)
或者换句话说,是否有任何保证在第一个成员之前没有填充?
当我们说一个程序泄漏内存,说新没有删除在C++中,它真的泄漏?我的意思是,当程序结束时,是否仍然将内存分配给某些非运行程序并且无法使用,或者操作系统是否知道每个程序请求的内存,并在程序结束时释放它?如果我多次运行该程序,我会耗尽内存吗?
我知道C/C++标准只能保证每个字符最少 8位,理论上9/16/42 /其他任何东西都是可能的,因此所有关于编写可移植代码的网站都会警告不要假设8bpc.我的问题是"非便携式"这是真的吗?
让我解释.在我看来,有三类系统:
char
不完全是 8位,我会感到非常惊讶.(如果我错了,请纠正我)char
位数比8位多,我不会感到非常惊讶,但我还没有听说过一个这样的系统(如果我不知道的话,请再次通知我)底线:是否有共同的(大于%0.001)平台(在类别1和2以上),其中char
是不是 8位?我的上述猜测是真的吗?
c++ ×7
c ×4
bits ×1
c++17 ×1
char ×1
function ×1
header-files ×1
lambda ×1
memory-leaks ×1
namespaces ×1
python ×1
python-3.x ×1
struct ×1