我有这段代码:
int foo() { return 0; }
int main()
{
int (*float_function)(float) = foo;
}
Run Code Online (Sandbox Code Playgroud)
x86-64 GCC 12.2
当使用, with编译时-Wall
,它会产生警告(链接):
警告:从不兼容的指针类型“int (*)()”初始化“int (*)(float)”[-Win兼容指针类型]
但是,当我从 更改float
为double
(链接)时:
int foo(){ return 0;}
int main()
{
int (*double_function)(double) = foo;
}
Run Code Online (Sandbox Code Playgroud)
现在警告消失了。
但我认为这两者都应该受到警告。
我有什么地方说错了吗?为什么 GCC 不抱怨第二个例子?
在这段代码中:
struct
{
auto operator[](const char*)
{
return *this;
}
} m_some_class;
Run Code Online (Sandbox Code Playgroud)
这里的类型是什么auto
?
在此代码中:
#include <iostream>
int main(void)
{
std::string {} = "hi";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这种类型的声明在 C++ 中有效。参见《Godbolt》。
作为信息,我测试了这个程序从c++11
到c++20
标志,因为扩展初始值设定项从以后可用c++11
。
我正在通过课程学习 Web 开发,现在是时候使用节点包管理器安装 Express 了。我必须安装express并使用以下命令:
npm install express
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
npm ERR! code EAI_AGAIN
npm ERR! errno EAI_AGAIN
npm ERR! request to https://registry.npmjs.org/express failed, reason: getaddrinfo EAI_AGAIN registry.npmjs.org
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\User\AppData\Roaming\npm-cache\_logs\2020-07-21T08_32_35_6
54Z-debug.log
Run Code Online (Sandbox Code Playgroud)
我现在完全迷失了。请不要评判我,因为我对 Node 还很陌生。非常感谢您的帮助。
为什么 C++ 提供复制构造函数?赋值运算符可以完成相同的任务。与赋值运算符相比,复制构造函数有什么优势吗?
我正在尝试在 aarch64 linux docker 容器中编译一个 rust 应用程序以在树莓派 4 上运行。除了包不是纯 rust 并使用 c 库时,我的工作正常。环形箱就是一个例子,当我尝试构建环形库而不设置时,ENV TARGET_CC=something
它失败并出现以下错误:
#18 32.35 Compiling ring v0.16.19
#18 38.13 error: failed to run custom build command for `ring v0.16.19`
#18 38.13
#18 38.13 Caused by:
#18 38.13 process didn't exit successfully: `/usr/src/content-manager/target/release/build/ring-902dd3bf18c6ec17/build-script-build` (exit code: 101)
#18 38.13 --- stdout
#18 38.13 OPT_LEVEL = Some("3")
#18 38.13 TARGET = Some("aarch64-unknown-linux-musl")
#18 38.13 HOST = Some("aarch64-unknown-linux-gnu")
#18 38.13 CC_aarch64-unknown-linux-musl = None
#18 38.13 CC_aarch64_unknown_linux_musl = None …
Run Code Online (Sandbox Code Playgroud) class Data {
double a, b, c;
};
int main() {
Data x, y, z;
cout << sizeof(x) << endl;
cout << &x << " " << &y << " " << &z << endl;
}
Run Code Online (Sandbox Code Playgroud)
上述代码的输出是:
24
0x7ffd911f5640 0x7ffd911f5660 0x7ffd911f5680
Run Code Online (Sandbox Code Playgroud)
我的问题是: Data 类对象只需要 24 字节的空间,那么为什么编译器为每个对象分配 32 字节(0x7ffd911f5640
和之间的内存空间)?0x7ffd911f5660
使用以下代码:
#include <stdio.h>
int main(void) {
printf("%c %c ", 82, 2130);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到输出:
R R
Run Code Online (Sandbox Code Playgroud)
怎么2130
转换成R
?
我想要得到的结果是将索引值与其他索引值相乘。例如在下面的代码中:我想通过 arr[0]*arr[1] 更新 arr[0] 值,通过 arr[9]*arr[9-8] 更新 arr[9] 值,对于剩余的索引,它将是 arr[i-1] * arr[i] * arr[i+1]。它在第一个索引上运行良好,但在其他索引上我得到了意想不到的结果。
#include<iostream>
using namespace std;
int main(){
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, j, k;
for(j = 0; j < 10; j++){
if(j == 0){
arr[j] = arr[j] * arr[j + 1];
}else if(j == 9){
arr[j] = arr[j] * arr[j - 1];
}else{
arr[j] = arr[j - 1] * arr[j] * arr[j + 1];
}
}
for(k = 0; …
Run Code Online (Sandbox Code Playgroud) 我有一个计时器的精简示例,我希望可以使用任何类型的可调用来实例化它。为了提高效率,是否建议预防性地将可调用对象移至数据成员中?
#include <concepts>
#include <cstdio>
#include <string>
#include <utility>
template <std::invocable Cb>
class timer {
public:
timer(Cb cb)
: cb_ { std::move(cb) }
{
}
auto call()
{
cb_();
}
private:
Cb cb_;
};
int main()
{
std::string something_to_print = "Hello World!\n";
timer some_timer([&]() { printf(something_to_print.c_str()); });
some_timer.call();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果移动或复制 lambda,我看不到程序集有任何差异。它有什么区别吗?