constexpr和之间有什么区别const?
这是在dev c ++ windows中编译的代码:
#include <stdio.h>
int main() {
int x = 5;
printf("%d and ", sizeof(x++)); // note 1
printf("%d\n", x); // note 2
return 0;
}
Run Code Online (Sandbox Code Playgroud)
x执行注释1后,我希望是6 .但是,输出是:
4 and 5
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么x在注1后没有增加?
我正在研究核心常量表达式*中允许的内容,这在C++标准草案的5.19 常量表达式第2段中有所描述:
条件表达式是核心常量表达式,除非它涉及以下之一作为潜在评估的子表达式(3.2),但是未评估的逻辑AND(5.14),逻辑OR(5.15)和条件(5.16)操作的子表达式不考虑[注意:重载的运算符调用函数.-end note]:
并列出随后的子弹中的排除项并包括(强调我的):
- 具有未定义行为的操作 [注意:包括,例如,有符号整数溢出(第5条),某些指针算术(5.7),除零(5.6)或某些移位操作(5.8) - 结束注释];
嗯?为什么常量表达式需要此子句来涵盖未定义的行为?常量表达式是否有一些特殊的东西需要未定义的行为才能在排除中进行特殊划分?
拥有这个条款是否给了我们没有它的任何优势或工具?
作为参考,这看起来像广义常量表达式提案的最新修订版.
我正在使用MinGW来编译C++ 11,我发现这不会引发错误:
int S;
cin>>S;
char array[S];
Run Code Online (Sandbox Code Playgroud)
虽然这样做("'数组'的存储大小未知"):
char array[];
Run Code Online (Sandbox Code Playgroud)
对我来说,第一种情况下的大小也是未知的,因为它取决于用户输入的内容.
据我所知,自动数组在编译时分配在堆栈内存中.那么为什么第一个例子不会失败呢?
#include <iostream>
using namespace std;
int main() {
int rows = 10;
int cols = 9;
int opt[rows][cols] = {0};
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
std::cout << opt[i][j] << " ";
}
std::cout << "\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
0 32767 1887606704 10943 232234400 32767 1874154647 10943 -1
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 …Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
int main(){
int n=10;
int a[n];
for (int i=0; i<n; i++) {
a[i]=i+1;
cout<<a[i]<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在Mac下的Xcode4中运行良好
如书中所说,这应该是错的,为什么?
好糊涂〜
我们已经知道,VLA(在C99中标准化)不是C++标准的一部分.
所以下面的代码在C++中是"非法的":
void foo(int n) {
int vla[n];
for (int i = 0; i < n; ++i) {
vla[i] = i;
}
}
Run Code Online (Sandbox Code Playgroud)
尽管编译器(g ++和clang ++)接受代码作为有效语法,但只有在启用情况下才会生成警告 .-pedantic
ISO C++禁止变长数组'vla'[-Wvla]
我的问题是:
为什么编译器接受该声明?
编译器不能只拒绝一个长度为的数组[is-no-know-at-compile-time]?
是否存在一种兼容性语法规则?
标准说了什么?
从生成的汇编代码中我看到编译器在循环中写入堆栈,就像普通数组一样,但我找不到任何关于标准行为的信息.
当我发现我已经使用了一段时间的匿名结构实际上只能在C11中使用,而不是C99时,我正在对我的项目进行编码,这是我想编写的标准.
给出以下代码:
struct data {
int a;
struct {
int b;
int c;
};
};
int main()
{
struct data d;
d.a = 0;
d.b = 1;
d.c = 2;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码应仅在C11中编译(或者如果编译器扩展提供此功能并且已启用).那么让我们看看不同编译器的结果:
compiler:
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix
command:
clang -std=c99 -Wall test.c -o test
result:
**OK**
Run Code Online (Sandbox Code Playgroud)
compiler:
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
command:
gcc -std=c99 -Wall test.c -o test
result:
**NOT OK**
test.c:6: …Run Code Online (Sandbox Code Playgroud) 这在C语言中有效吗?
#include <stdio.h>
int main()
{
int i = 5;
int a[i]; // Compiler doesn't give error here. Why?
printf("%d",sizeof(a)); //prints 5 * 4 =20. 4 is the size of integer datatype.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器不会在声明中给出错误int a[i];.我不是一个常数,那么它怎么能成功编译?是因为我使用的是gcc编译器吗?是否允许在C++中使用?
我刚刚从C进入C++
在C(89/90)中,a const实际上不是常量(与#define'd enum,或文字相对),而是一旦设置为只读.即,我可以:
const int x = rand();
Run Code Online (Sandbox Code Playgroud)
这很好 - x直到运行时才知道这一点.因此,我不能
int arr[x]; // error - x is not a compile-time constant
Run Code Online (Sandbox Code Playgroud)
然后,C标准之一(99?)继续进行并允许可变长度数组.虽然我通常使用C语言中的ANSI标准编码,但实际上这已经产生了影响,因为我正在尝试拾取C++ 11.
据我所知,C++不允许使用可变长度数组.但是,许多编译器允许它作为扩展(GCC?).问题是,现在我想学习C++ 11,如果有什么我编码为有效的C我不能告诉++或C++扩展与C99兼容.例如:
std::default_random_engine e{};
std::uniform_int_distribution<int> d{};
const int x{d(e)};
int arr[x]; // compiles
Run Code Online (Sandbox Code Playgroud)
我不知道这是否是有效的C++.显然,x直到运行时才知道值.我想我可能不明白C和C++之间的区别const?