g ++不报告给定的代码错误? [英] How does g++ not report error with the given code?

查看:141
本文介绍了g ++不报告给定的代码错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对



我能够使用g ++ 4.7.3来成功编译和构建下面的程序。

  #include< iostream> 
using namespace std;

int elapsedtime(int time1,int time2)
{
return(time2-time1);
}

int main()
{
int time1;
int time2;
cin>>时间1>> time2;
cout<< 您的经过时间是< elapsedtime<< endl;
}

main 是:

  cout< 您的经过时间是<经过时间(time1,time2)<< endl; 

g ++如何编译第一个版本没有错误?


< std :: ostream 有一个运算符<< (bool),函数名可隐式转换为标准下的 bool (通过函数到指针的转换后跟布尔转换) 。相关语言是(§4[conv] / p1,§4.3[conv.func],§4.12[conv.bool]):


A 标准转换序列是标准转换序列
,顺序如下:




  • 从以下集合开始的零或一次转换:从值到值的转换,数组到指针的转换和函数到指针的转换。

  • 从下列集合中进行零个或一个转换

  • 零个或一个合格转换。
  • / li>


函数类型T的左值可以转换为类型$ pr $ b的指针结果是指向函数的指针。



一个prvalue算术,无范围枚举,指针或指向
成员类型的指针可以转换为prvalue键入 bool 。零
值,空指针值或空成员指针值将
转换为 false ;任何其他值将转换为 true



This is a continuation of my answer to why is elapsedtime giving me an output of 1?

I was able to successfully compile and build the following program using g++ 4.7.3.

#include <iostream>
using namespace std;

int elapsedtime(int time1, int time2)
{
   return (time2-time1);
}

int main()
{
   int time1;
   int time2;
   cin >> time1 >> time2;
   cout << "Your elapsed time is " << elapsedtime <<endl;
}

The intent of the last line in main is:

   cout << "Your elapsed time is " << elapsedtime(time1, time2) <<endl;

How is g++ able to compile the first version without error?

解决方案

std::ostream has an operator << (bool), and function names are implicitly convertible to bool under the standard (by a function-to-pointer conversion followed by a boolean conversion). The relevant language is (§4 [conv]/p1, §4.3 [conv.func], §4.12 [conv.bool]) :

A standard conversion sequence is a sequence of standard conversions in the following order:

  • Zero or one conversion from the following set: lvalue-to-rvalue conversion, array-to-pointer conversion, and function-to-pointer conversion.
  • Zero or one conversion from the following set: integral promotions, floating point promotion, integral conversions, floating point conversions, floating-integral conversions, pointer conversions, pointer to member conversions, and boolean conversions.
  • Zero or one qualification conversion.

An lvalue of function type T can be converted to a prvalue of type "pointer to T." The result is a pointer to the function.

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

这篇关于g ++不报告给定的代码错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆