我有以下代码
#include <iostream>
void foo(const int* const &i_p) {
std::cout << &i_p << std::endl;
}
int main () {
int i = 10;
int* i_p = &i;
std::cout << &i_p << std::endl;
foo(i_p);
}
Run Code Online (Sandbox Code Playgroud)
x86-64 clang 9.0.0
输出上是
Program returned: 0
0x7ffc43de63f8
0x7ffc43de6400
Run Code Online (Sandbox Code Playgroud)
x86-64 clang 10.0.0
而在编译器资源管理器链接上,输出变为
Program returned: 0
0x7ffc9da01ef0
0x7ffc9da01ef0
Run Code Online (Sandbox Code Playgroud)
这里进行了什么优化来提供相同的地址?我相信应该实现一个临时对象,因为我们无法将低级const
指针绑定到低级non-const
。
c++ const-correctness language-lawyer clang++ reference-binding
我有一个关于类声明与类定义的快速问题。
因此,我可以想到四种不同的类代码方案:
哪些符合声明,哪些符合定义?为什么?
TIA。
1)
class foo_empty{};
Run Code Online (Sandbox Code Playgroud)
2,3)
class foo_NoMemDef{
int f1(); //Member function declaration(?), no body
//int i; (consider including this as a separate case 3)
};
Run Code Online (Sandbox Code Playgroud)
4)
class foo_final{
int f2(){}
};
Run Code Online (Sandbox Code Playgroud) Going through Modern C++ Design by Andrei Alexandrescu, I am unable to understand the reason for using unnamed, unscoped enum
, to store the non-type parameter.
Why not use a variable directly.
Does it have any advantages?
template <int v>
struct Int2Type
{
enum { value = v }; //why not use int value = v; which compiles fine
};
Run Code Online (Sandbox Code Playgroud)
Extra (if that helps): The template is intended to be used as a "type generator" to select different functions at …
在阅读C ++ Primer 5th Edition时,我对语法的确切含义感到困惑。特别是,我对less <>和hash <>中的模板参数列表后的空白()感到好奇。
hash<string>()(s.bookNo); // () after <string>
less<T>()(v1, v2);
Run Code Online (Sandbox Code Playgroud)
我猜想它在最后一组()即(s.bookNo)中创建一个函数实例化并传递参数
提前致谢。
我的问题是泄漏的内存如何标记为“正在使用”。根据我的理解,每个程序都会为其所有内存需求(堆栈、堆、只读、程序内存)获得一块连续的内存。我想一旦程序完成,所有这些都将被释放(释放 = 打开新程序以供使用)。现在,即使我在程序中有内存泄漏,当程序内存块被释放时,它不会自动释放吗?
我遇到过有关此问题的问题,其中说操作系统必须将其释放,但在某些较旧的操作系统版本中,这不会发生。鉴于第一段中提供的解释,我不明白为什么这会成为一个问题。此外,没有人谈论“释放”实际上意味着/做什么。
我很好奇为什么{}
不允许在任何函数(在全局范围内)之外创建块范围。
我可以执行以下操作:
int main(){
{
int i = 4;
}
//other stuff
}
Run Code Online (Sandbox Code Playgroud)
但不是
{
int i = 4;
}
int main(){
//other stuff
}
Run Code Online (Sandbox Code Playgroud)
这个问题激起了我在实现编译时声明时的兴趣,为此我创建了一个变量,在声明逻辑完成后(释放内存)将立即销毁该变量。