我记得经常被告知C预处理器不知道C.它不知道类型,不记得声明的符号等.如果是这样的话,它怎么可能根据其参数的类型采取不同的行为?
下面的代码应该询问用户他的体重,如果用户输入> 15,它应该执行一个while循环将其增加到50代码编译成功,但无论你输入> 15它都不会执行while循环.
#include <iostream>
using namespace std;
int main()
{
int weight;
cout << "Current weight?" << endl;
cin >> weight;
cout << "Your weight:" << weight << endl;
if (weight > 15) {
while (weight == 50) {
weight = weight + 1;
cout << "Weight:" << weight << endl;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有我的main()函数的翻译单元和另一个没有 main 的翻译单元。假设即使我只控制第二个而无法触摸第一个。
现在,由于我不会讨论的原因,我希望能够在运行之前运行一些代码main()。我知道这可以通过使用函数调用初始化全局变量来完成,但我想隐藏这一点 - 尽可能少地使用宏(我敢说不使用宏吗?可能不可能,C++ 中没有适当的静态块)
什么是优雅的,或者我们可以说,不是很丑的做这件事的方式?更清楚地说,我正在寻找可以多次使用此功能的东西,而不仅仅是让它工作一次的东西。我希望它尽可能接近:
// ... at global scope ...
static {
// my code here
}
Run Code Online (Sandbox Code Playgroud)
PS:这个问题与这个关于初始化静态类成员的问题有关,但不一样。它还希望清楚地反驳这个声明,它不能在 C++ 中完成。
注意:是的,我知道静态初始化顺序失败,不需要提醒我……而且我不是要求绕过它的东西。显然,静态运行代码需要一些谨慎。
我有以下内容:
class A { ... };
class B : public A { ... };
// ...
B b;
const A& aref(b);
// ...
const B& bref(aref);
Run Code Online (Sandbox Code Playgroud)
当我编译时,我得到:
no suitable user-defined conversion from "const A" to "const B" exists
Run Code Online (Sandbox Code Playgroud)
现在,如果这些是指针而不是引用,我会使用
bptr = dynamic_cast<B*>(aptr);
Run Code Online (Sandbox Code Playgroud)
但参考文献没有.我该怎么办?切换到指针?别的什么?
我有一些POD struct foo; 假设是的struct foo { int x; unsigned y; }.我希望能够struct foo使用词典顺序进行比较- 当然是按照他们的字段顺序进行比较.也就是说,我希望所有的运营商<,==>等为工作struct foo的
我可以用一些通用的方式做到这一点,而没有用任何反射巫术装饰我的结构定义- 而且没有拼写出所有那些操作符定义?或者是否有能力做到这一点太依赖"语言反思"的期望?
我正在写一个小型库,它有一些API函数返回两个东西(不同类型).我宁愿不为此声明一个结构; 所以我想回来一个std::pair<foo, bat>.但是 - 也许在这些现代时代我宁愿回来std::tuple<foo, bar>呢?
更一般地说,什么时候应该优先选择元组,何时配对更合适的构造?
如果我跑:
#include <type_traits>
#include <iostream>
int main()
{
std::cout << "sizeof(long) = " << sizeof(long) << "\n";
std::cout << "sizeof(long long) = " << sizeof(long long) << "\n";
std::cout << "std::is_same<long, long long>::value = "
<< std::boolalpha << std::is_same<long, long long>::value
<< "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我的机器上(和Coliru),我得到:
sizeof(long) = 8
sizeof(long long) = 8
std::is_same<long, long long>::value = false
Run Code Online (Sandbox Code Playgroud)
它不仅仅是std::is_same<>; 翻译单元期待的一个实例int64_t失败,因为我只编译int并long long int在另一个翻译单元,尽管这一切发生在同一台机器上.
为什么这些类型不一样?我的意思是,表现出你所拥有typedef的相同类型的行为?
如果我使用clang 3.8.1编译:
extern "C" {
int foo(int x) { register int y = x; return y; }
}
int main() { return foo(123); }
Run Code Online (Sandbox Code Playgroud)
我收到警告:
a.cpp:3:18: warning: 'register' storage class specifier is deprecated and incompatible with C++1z [-Wdeprecated-register]
int foo(int x) { register int y = x; return y; }
^~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
...我真的不应该得到,因为内部函数是C代码.如果我使用GCC 6.3.1,即使有-Wall,我也不会收到此警告.
这是一个铿锵的错误还是我做错了什么?
这是一个重复的问题,但无法在任何地方找到正确的解释.
我有以下代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct student_data_boys
{
int roll_b;
char name_b[4];
} student_data_boys;
int main()
{
student_data_boys *ptr;
int i;
int num = 5;
ptr = (student_data_boys *) malloc(num * sizeof(student_data_boys));
if(ptr == NULL)
printf("Can't allocate memory.\n");
exit(1);
}
printf("Allocated memory space size is : %ld bytes.\n",
(num * sizeof(student_data_boys)));
for(i=0;i<num;i++) {
printf("Start address of data1[%d] is : %p.\n", i+1, ptr+i);
}
free(ptr);
}
Run Code Online (Sandbox Code Playgroud)
当我递增结构指针时,预期的下一个地址是基地址加上结构块的大小(这里是8),但是当我看到输出时,下一个大小不是预期的.
产出如下:
Allocated memory space is : 40.
Start address of data1[1] is …Run Code Online (Sandbox Code Playgroud) 我需要对程序的标准输出流做两件事:比如说,将它导入两个管道,或者将它打印到终端并将其导入管道。但是 - 这些东西都没有将它定向到文件中。
如果我想要一个管道 + 一个文件,我会使用tee命令:myprog | tee out.txt | another_command,如解释here。但是如果这两个操作都没有写入文件呢?
c++ ×7
c ×2
64-bit ×1
bash ×1
c++11 ×1
c11 ×1
clang++ ×1
downcast ×1
dynamic-cast ×1
extern-c ×1
generics ×1
idiomatic ×1
idioms ×1
inheritance ×1
int64 ×1
long-long ×1
multilingual ×1
pipe ×1
pointers ×1
reference ×1
reflection ×1
shell ×1
static-block ×1
std-pair ×1
stdtuple ×1
structure ×1
tee ×1
types ×1
while-loop ×1