我在cppreference.com上看到了以下示例
int x; // OK: the value of x is indeterminate
int y = x; // undefined behavior
Run Code Online (Sandbox Code Playgroud)
这里int y = x;是未定义的行为,因为它x是未初始化的.
但,
unsigned char c; // OK: the value of c is indeterminate
unsigned char d = c; // OK: the value of d is indeterminate
Run Code Online (Sandbox Code Playgroud)
这里unsigned char d = c;是不确定的行为,但unsigned char c;也是一个未初始化的变量.
那么,为什么unsigned char d不确定的价值呢?
我对以下代码感到困惑:
#include <iostream>
using namespace std;
int *foo()
{
//Operation
}
int main ()
{
auto int ret = foo();
}
Run Code Online (Sandbox Code Playgroud)
我在GCC下编译了上面的代码,但是我收到了以下错误:
error: two or more data types in declaration of 'ret'
auto int ret = foo();
Run Code Online (Sandbox Code Playgroud)
但是,如果我删除int类型,如下:
auto ret = foo();
Run Code Online (Sandbox Code Playgroud)
然后它成功运行.
auto是一个存储类,int是一种数据类型,那么为什么我在第一种情况下得到"两种或更多数据类型"的错误?
我在GCC中编译了以下程序C++14.
#include <iostream>
using namespace std;
auto func(int i);
int main()
{
auto ret = func(5);
return 0;
}
auto func(int i)
{
if (i == 1)
return i;
else
return func(i-1) + i;
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误.
Run Code Online (Sandbox Code Playgroud)In function 'int main()': 8:16: error: use of 'auto func(int)' before deduction of 'auto' auto ret = func(5);
那么,我在这里错过了什么?
我已经看到匿名类在C++上Quora的代码.它已成功编译并运行.
代码在这里:
#include <iostream>
auto func()
{
class // no name
{
public:
int val;
} a;
a.val = 5;
return a;
}
int main()
{
std::cout << func().val << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
那么,它在C++中是否有效?
另外,我很想知道,是否可以在C++中使用匿名类?
我阅读了有关std::vector使用cppreference的演绎指南.
例:
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
std::vector x{v.begin(), v.end()}; // uses explicit deduction guide
}
Run Code Online (Sandbox Code Playgroud)
所以,我有一些问题:
什么是std::vectorC++ 17 中的演绎指南?
为什么以及何时需要向量推导?
在这里,是x一个std::vector<int>还是一个std::vector<std::vector<int>>?
我编写了以下隐藏成员函数模板的代码.
#include <iostream>
struct A
{
template<int> void func()
{
std::cout<<"Function tamplate of A"<<std::endl;
}
};
struct B : A
{
template<char> void func()
{
std::cout<<"Function tamplate of B"<<std::endl;
}
using A::func;
};
int main()
{
B().func<0>();
}
Run Code Online (Sandbox Code Playgroud)
这个程序在Clang编译器中工作.现场演示Clang
但是,GCC编译器会产生歧义错误.现场演示GCC
source_file.cpp: In function ‘int main()’:
source_file.cpp:22:17: error: call of overloaded ‘func()’ is ambiguous
B().func<0>();
Run Code Online (Sandbox Code Playgroud)
那么,哪个编译器是正确的?
我读到了关于未定义行为的答案,我看到以下声明:
++++++i; // UB, parsed as (++(++(++i)))
Run Code Online (Sandbox Code Playgroud)
我认为这不是未定义的行为.我有一个疑问,它在C++中真的是UB吗?如果是,那怎么样?
此外,我制作程序并使用g++ prog.cpp -Wall -Wextra -std=gnu++1z -pedantic命令编译,它没有任何警告工作正常.它给出了预期的输出.
#include <iostream>
using namespace std;
int main()
{
int i = 0;
cout<<++++++i<<endl;
}
Run Code Online (Sandbox Code Playgroud) 在Mac上使用GCC版本4.2.1(Apple Inc. build 5664)时,以下小程序非常笨拙.
#include <stdio.h>
int main(){
int x = 1 << 32;
int y = 32;
int z = 1 << y;
printf("x:%d, z: %d\n", x, z);
}
Run Code Online (Sandbox Code Playgroud)
结果是x:0, z: 1.
知道为什么x和z的值不同吗?
非常感谢.
if constexpr()和之间有什么区别if()?
我何时何地可以同时使用它们?
我%qd在浏览github代码时看到了格式说明符.然后我检查了GCC编译器,它运行正常.
#include <stdio.h>
int main()
{
long long num = 1;
printf("%qd\n", num);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
什么是格式说明目的%qd的printf()?