考虑以下程序:
#include <iostream>
int main()
{
int n = 3;
int fact = 1;
for(auto i{1};i<=n;i++)
fact*=i;
std::cout<<"fact of "<<n<<" is "<<fact;
}
Run Code Online (Sandbox Code Playgroud)
即使我使用-std=c++14选项,它也可以在ideone上编译.在这里查看现场演示.但是在C++ 14中,变量i应该initializer_list根据此推断出来.
有一个C++ 1z的提议,它为括号初始化实现了新的类型推导规则:
对于直接列表初始化:
对于只包含单个元素的braced-init-list,自动扣除将从该条目中推断出来;
对于具有多个元素的braced-init-list,自动扣除将是不正确的.
[例:
auto x1 = {1,2}; // decltype(x1)是std :: initializer_list
auto x2 = {1,2.0}; //错误:无法推断出元素类型
auto x3 {1,2}; //错误:不是单个元素
auto x4 = {3}; // decltype(x4)是std :: initializer_list
auto x5 {3}; // decltype(x5)是int.
- 结束例子]
所以,规则改变了C++ 17.因此,当我使用时,程序不应该编译-std=c++14.这是g ++中的错误吗?这个变量不应该i像 …
#include <iostream>
#include <type_traits>
int main()
{
struct T{ virtual void foo()=0;};
std::cout<<std::boolalpha;
std::cout<<std::is_array<int[3]>::value<<'\n';
std::cout<<std::is_array<T>::value<<'\n';
std::cout<<std::is_array<T1[2]>::value<<'\n';
std::cout<<std::is_array<T[3]>::value<<'\n'; // why uncommenting this line causes compile time error?
}
Run Code Online (Sandbox Code Playgroud)
我知道创建抽象类的对象是不可能的.这里T是抽象的,因此不可能创建struct T的对象.但请考虑以下语句
std::cout<<std::is_array<T[3]>::value<<'\n';
Run Code Online (Sandbox Code Playgroud)
为什么它会给我一个错误?该语句仅检查给定类型是否为数组.是否意味着如果T是静态成员的阵列&值value的计算结果为true然后对象数组将被创建?但是,为什么需要在这里创建数组呢?什么是创建数组的需要如果我不能使用该数组?这不仅仅是记忆的浪费吗?
那么为什么以下语句不会给出任何编译器错误?
std::cout<<std::is_array<T>::value<<'\n';
Run Code Online (Sandbox Code Playgroud)
我在这里理解错了什么?请帮我.
我知道 C 和 C++ 是由不同委员会标准化的不同语言。
我知道像 C 一样,效率从一开始就是 C++ 的主要设计目标。所以,我认为如果任何特性不会产生任何运行时开销,并且它是有效的,那么它应该被添加到语言中。该C99标准有一些非常有用和高效的特性,其中之一是复合文字。我在这里阅读了有关编译器文字的信息。
以下是一个程序,显示了复合文字的使用。
#include <stdio.h>
// Structure to represent a 2D point
struct Point
{
int x, y;
};
// Utility function to print a point
void printPoint(struct Point p)
{
printf("%d, %d", p.x, p.y);
}
int main()
{
// Calling printPoint() without creating any temporary
// Point variable in main()
printPoint((struct Point){2, 3});
/* Without compound literal, above statement would have
been written …Run Code Online (Sandbox Code Playgroud) 考虑以下简单程序:
#include <iostream>
void foo() { }
int main() {
std::cout<<static_cast<void*>(foo);
}
Run Code Online (Sandbox Code Playgroud)
它编译罚款VC++,但g++与clang++给编译错误.
在这里看现场演示(VC++)
在这里看现场演示(clang++)
在这里看现场演示(g++)
诊断由g++&给出clang++:
source_file.cpp: In function ‘int main()’:
source_file.cpp:4:38: error: invalid static_cast from type ‘void()’ to type ‘void*’
std::cout<<static_cast<void*>(foo);
^
Run Code Online (Sandbox Code Playgroud)
那么,问题是根据C++标准,哪个编译器就在这里?我认为,行为g++和clang++正确这里.我知道我应该reinterpret_cast在这里使用而不是static_cast.这是VC++编译器中的错误吗?如果答案取决于C++的具体标准,那么我也很想知道它.
考虑以下计划:
#include <cstdio>
#include <cmath>
int main()
{
int d = (int)(abs(0.6) + 0.5);
printf("%d", d);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g++7.2.0输出0(参见此处的现场演示)
g++6.3.0(参见此处的现场演示)
prog.cc: In function 'int main()':
prog.cc:6:26: error: 'abs' was not declared in this scope
int d = (int)(abs(0.6) + 0.5);
^
prog.cc:6:26: note: suggested alternative:
In file included from prog.cc:2:0:
/opt/wandbox/gcc-6.3.0/include/c++/6.3.0/cmath:103:5: note: 'std::abs'
abs(_Tp __x)
^~~
Run Code Online (Sandbox Code Playgroud)
clang++5.0.0输出1(在此处查看现场演示)
clang++3.6.0(点击此处观看现场演示)
prog.cc:6:19: error: use of undeclared identifier 'abs'; did you …Run Code Online (Sandbox Code Playgroud) 我很想知道为什么C++中不允许使用以下内容?
第一个项目:
#include <iostream>
class Test {
public:
int myfun();
}
virtual int Test::myfun()
{ return 0; }
int main()
{ }
Run Code Online (Sandbox Code Playgroud)
[错误]'虚拟'外部类声明
第二个项目:
#include <iostream>
class Test {
public:
int myfun();
};
static int myfun() {
std::cout<<"This program contains an error\n";
return 0;
}
int main() {
Test::myfun();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
[Error]无法在没有对象的情况下调用成员函数'int Test :: myfun()'
所以,我的问题是
为什么我不能像第一个程序那样使成员函数虚拟化?
为什么我不能像第二个程序那样使成员函数静态化?
有没有理由在课外不允许这两个关键词?
在java中,有命令行选项javac -version来了解计算机上安装的版本.我很想知道有没有办法知道gcc的版本或任何可以帮助我的命令行选项?
记住我不是在问C99,C11,C++ 11等语言标准.我问的是像gcc 4.9.2等编译器版本.
考虑这个程序:
#include <iostream>
int main()
{
delete std::cout;
}
Run Code Online (Sandbox Code Playgroud)
AFAIK转换函数operator void*()const已从C++ 11中删除.因此,该程序在C++ 11编译器上的编译失败.雅,这是真的,g ++ 4.8.1和4.9.2都给出了诊断(以警告的形式,删除void*未定义,这也是好事).但是这个程序不应该在编译时失败,因为删除了转换函数,因为在C++ 98和C++ 03中,所有流对象都可以隐式转换为void*.这个错误吗?他们仍然没有实施这一改变似乎有点令人惊讶.
我在g ++ 4.9.2(支持C++ 14)中尝试过这个程序,但是它提供了警告而不是编译器错误.Ideone编译器按预期给出了错误.(在这里查看现场演示)
在C&C++中,如果右操作数在使用>>和<<(右移和左移操作符)时为负,则程序的行为未定义.考虑以下计划:
#include <iostream>
int main()
{
int s(9);
std::cout<<(s<<-3);
}
Run Code Online (Sandbox Code Playgroud)
g ++给出以下警告:
[Warning] left shift count is negative [enabled by default]
Run Code Online (Sandbox Code Playgroud)
MSVS 2010发出以下警告:
warning c4293: '<<' : shift count negative or too big, undefined behavior
Run Code Online (Sandbox Code Playgroud)
现在我好奇Java和C#会发生什么?
我试过以下程序
class left_shift_nagative
{
public static void main(String args[])
{
int a=3;
System.out.println(a<<-3);
System.out.println(a>>-3);
}
}
Run Code Online (Sandbox Code Playgroud)
计划成果:
1610612736
0
Run Code Online (Sandbox Code Playgroud)
C#轮到:
namespace left_shift_nagative
{
class Program
{
static void Main(string[] args)
{
int s = 3;
Console.WriteLine(s << -3);
Console.WriteLine(s >> …Run Code Online (Sandbox Code Playgroud) 考虑以下计划:
#include <iostream>
int main()
{
int num=345;
std::cout<<"num " + num<<'\n';
}
Run Code Online (Sandbox Code Playgroud)
当我运行该程序时,它显示F作为输出.怎么样?是否在这里执行指针运算?如果我使用 - 符号而不是+,则会出现空白输出.