|
|
Classification: |
C++ |
Category: |
Development |
Created: |
10/19/2000 |
Modified: |
09/11/2002 |
Number: |
FAQ-0529 |
Platform: |
Not Applicable |
|
Question: How do I use _LIT() constants with the ?: operator?
Answer: Here's the code snippet in question:
_LIT(KDictName_zy, "zy.dct"); _LIT(KDictName_pinyin, "pinyin.dct"); ... err = iFile.Open(aRfs, iUseZY?KDictName_zy:KDictName_pinyin, EFileStream);
When this is compiled, the C++ compiler will object with an error message:
ambiguous overload for `bool ? const TLitC<7> & : const TLitC<11> &'
This message reflects the C++ rule that the type of the "bool ? X:Y" expression is the type of the X expression: either X
and Y must be the same type, or there must be a conversion from Y to type X. The _LIT() macro uses a C++ template, so it produces
a different type for each possible string length.
How To Solve the Problem:
Happily, the TLitC<> class has an operator() function which presents the object as a const TDesC&, so the fix is to change
the code to
err = iFile.Open(aRfs, iUseZY?KDictName_zy():KDictName_pinyin(), EFileStream);
The operator() function is an inline function which just reassures the C++ compiler that everything is alright: it doesn't
actually generate any extra code.
Footnote:
The old _L() macro produces a TPtrC object, so the two expressions had the same type. Unlike the _LIT() macro, which arranges
to do all of the work at compile time, the _L() macro calls a TPtrC constructor at runtime, so it makes your code bigger and
slower.
|
|
|