在全局命名空间中给出以下声明:
constexpr int x = x;
Run Code Online (Sandbox Code Playgroud)
这个结构良好吗?
草案C++ 14标准部分3.6.2 [basic.start.init]说:
具有静态存储持续时间(3.7.1)或线程存储持续时间(3.7.2)的变量应在任何其他初始化发生之前进行零初始化(8.5).[...]
似乎使得示例定义良好的是x在常量初始化期间使用其自己的值初始化,这将0归因于零初始化.
error: the value of 'x' is not usable in a constant expression
constexpr int x = x;
^
Run Code Online (Sandbox Code Playgroud) 可能重复:
主要的正确声明是什么?
如果没有特别引用任何代码,我正在寻找以下示例的解释:
#include <iostream>
int main()
{
std::cout << "Hello world" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白是什么return 0.你能用尽可能简单的英语解释一下吗?
#include <iostream>
int main(int argc, char** args) {
int foo = foo + 4;
std::cout << foo << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
还有一个后续问题,是否有一个编译器标志来阻止这种事情?我发现-Wall有时会工作,最好是完全防止它.
My compiler:
g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
Run Code Online (Sandbox Code Playgroud) 我正在浏览http://geeksforgeeks.org/?p=10302上的代码
#include<stdio.h>
int initializer(void)
{
return 50;
}
int main()
{
static int i = initializer();
printf(" value of i = %d", i);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码不会在C中编译,因为静态变量需要在main()启动之前初始化.那样就好.但是这段代码在C++编译器中编译得很好.
我的问题是,当静态在两种语言中具有相同的用法时,为什么它在C++编译器中编译.当然编译器对于这些语言会有所不同,但我无法确定原因.如果在标准中指定,我很想知道.
我在SO上搜索了这个问题,找到了3个类似的链接但是徒劳无功. Link1 Link2 Link3
谢谢你的帮助.
可能重复:
主要回报是什么?
例如,以下代码编译时没有任何警告:
#include <stdio.h>
int i = i + 1;
int main(int argc, char *argv[])
{
fprintf (stderr, "%d\n", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我认为这在语法上是非法的,因为i它在声明之前使用,是不是?
在我看来,外观int i = i + 1;肯定是一个错误,为什么编译器没有警告呢?我使用gcc 4.5.1.
我试图像这样初始化一个bool数组:
bool FcpNumberIsOk[MAX_FCPS]={true};
Run Code Online (Sandbox Code Playgroud)
但是当我调试它时,我只看到数组的第一个元素被初始化,其他元素都是假的.怎么会这样?我在ubuntu 10上使用Qt,初始化是在方法内的本地数组上完成的.
好的,谢谢你的回答.
为什么第一个注释行正确编译,而第二个没有?
为什么可以a将自己作为构造函数参数,但b不能?
这两个人做的不一样吗?
class Foo { Foo &operator =(Foo const &); /* Disable assignment */ };
int main()
{
Foo a = a; // OK
Foo b(b); // error C2065: 'b' : undeclared identifier
}
Run Code Online (Sandbox Code Playgroud)
由于它看起来像编译器依赖,似乎问题比我想象的更严重.
所以我想问题的另一部分是,以下代码是否有效?
它在GCC中出错,但Visual C++执行得很好.
int main()
{
int i = 0;
{ int *i(&i); }
return i;
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
这个自我初始化有效吗?
这是一个定义明确的C/C++程序吗?
int foo = foo;
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
将foo被初始化为零,或者是不确定的行为?
我有一个用C语言编写的库.在代码中,我发现了几行代码int x = x;.我需要用/ Zw标志重写所有这些代码以进行编译.在某些意味着的地方int x = some_struct->x;,但在另一些情况下,我不明白它是什么.在某些地方,它首先使用x变量.所以在哪些情况下可以使用这种int x = x;表达方式.
void oc_enc_tokenize_dc_frag_list(oc_enc_ctx *_enc,int _pli,
const ptrdiff_t *_coded_fragis,ptrdiff_t _ncoded_fragis,
int _prev_ndct_tokens1,int _prev_eob_run1){
const ogg_int16_t *frag_dc;
ptrdiff_t fragii;
unsigned char *dct_tokens0;
unsigned char *dct_tokens1;
ogg_uint16_t *extra_bits0;
ogg_uint16_t *extra_bits1;
ptrdiff_t ti0;
ptrdiff_t ti1r;
ptrdiff_t ti1w;
int eob_run0;
int eob_run1;
int neobs1;
int token;
int eb;
int token1=token1;
int eb1=eb1;
/*Return immediately if there are no coded fragments; otherwise we'd flush
any trailing EOB run into the …Run Code Online (Sandbox Code Playgroud) c++ ×8
c ×4
arrays ×1
c++14 ×1
c++17 ×1
coding-style ×1
constexpr ×1
constructor ×1
definition ×1
gcc ×1
static ×1
syntax ×1