Re: sizeof()
On Mar 7, 9:01 am, "maverick" <mod...@gmail.com> wrote:
The following program's output is 4 bytes on g++(v3.2.2) RH Linux (32
bit).
on printing, individually, the sizeof(g()) and sizeof(h()) it o/ps 4
and 1 respectively.
I am wondering what the standard says about sizeof ( expr?expr:expr)
It says that it will be the size of the resulting type. Like
every expression in C++, a ?: expression has a statically
evaluated type.
#include<iostream>
using namespace std;
bool f()
{
return false;
}
int g()
{
return 1;
}
char h()
{
return 's';
}
int main(int a, char* b[])
{
if ( a == 1 )
cerr << sizeof(f()?h():g()) <<endl;
else
cerr << sizeof(f()?g():h()) << endl;
}
Both line should output the same thing as sizeof(int), since
that is the type of the ?: expression in both cases.
Note that you can also check this by using function overloading:
void
showType( int )
{
std::cout << "int" << std::endl ;
}
void
showType( char )
{
std::cout << "int" << std::endl ;
}
showType( condition ? g() : h() ) ;
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]