Hello Christian, Thank you for your message. In fact, this is kind of an intentional difference which I thought this would be convenient for a user. Handling string in such a way is useful when you want to write a real quick script. Let me clarify each case. 1) char *vName; switch(vName) { case "name1": ... } This is an illegal C/C++ code. Argument to a switch statement has to be an integer constant. It is possible to detect and reject this case in Cint. I am not sure if I should do this. 2) if(vName == "name1") { ... } This , in fact, is a valid C/C++ code. However, the way a compiler or an interpreter handles string constant may vary the result. Strictly speaknig both g++ and Cint complies with the C/C++ behavior, but the code relies on an unguaranteed behavior of C/C++. Cint maintains a table of string constant. If there apears an identical string constant, it is referenced from the same table location. This guarantees if content of string is the same, we will have same address for that. So, "name1"=="name1" is true. But for most of the compilers, this is not guaranteed. And C/C++ leaves freedom of handling such a case. Some compilers may warn you this ambiguity, some are not. Thank you Masaharu Goto >Date: Thu, 22 Feb 2001 23:00:03 +0100 >From: cstrato@EUnet.at >Reply-To: cstrato@EUnet.at >To: "roottalk@pcroot.cern.ch" <roottalk@pcroot.cern.ch> >Subject: [ROOT] Difference between CINT and compiler > >Dear Rooters > >Since I am testing most of my code as macro before using it in a >library, >I came across the following differences between interpreted and compiled >code: > >1, switch statement: >void testcase(const char *vName) >{ > switch(vName) { > case "name1": > cout << "ok: " << vName << endl; > break; > default: > cout << "error: " << vName << endl; > }//switch >} > >This code works fine as function testcase("name1") in a macro, however, >the gcc >compiler produces an error, since each case label must be a constant >expression >of an integral type. > >2, if statement: >void testif(const char *vName) >{ >//only ok in macro > if (vName == "name1") { > cout << "ok: " << vName << endl; > } else { > cout << "error: " << vName << endl; > }//if >//ok when compiled > if (strcmp(vName,"name1") == 0) { > cout << "ok: " << vName << endl; > } else { > cout << "error: " << vName << endl; > }//if >} > >Once again, this code works fine as function testif("name1") in a macro, >it does >also compile without problems, however, the first "if" expression gives >always >"error: name1" while the second "if" expression works as expected. > >Why is this difference between CINT and compiled code? Is it necessary? >(It is somehow frustrating to find out that tested code breaks, once it >is compiled) > >My system is: LinuxPPC with gcc2.95.2-2a and root 3.00/01 > >Thank you in advance for your help >Christian >---------------------------------- >C.h.r.i.s.t.i.a.n S.t.r.a.t.o.w.a >V.i.e.n.n.a. A.U.S.T.R.I.A >e-mail: cstrato@EUnet.at > >
This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:38 MET