[Development] Compiler warnings

Ziller Eike Eike.Ziller at theqtcompany.com
Mon Oct 20 09:05:12 CEST 2014


On Oct 17, 2014, at 7:26 PM, Mathias Hasselmann <mathias at taschenorakel.de> wrote:

> 
> 
> Am 17.10.2014 um 18:31 schrieb Thiago Macieira:
>> On Friday 17 October 2014 13:06:39 Milian Wolff wrote:
>>> enum Foo {
>>> Bar = 1, Baz = 2
>>> };
>>> 
>>> Foo foo = static_cast<Foo>(3);
>>> 
>>> Now what do you do without a default clause?
>> 
>> Shoot the developer who abused the API.
>> 
>> If the function accepts enum values 1 and 2 and you pass a 3, you deserve the
>> undefined behaviour.
>> 
>> That is the same as passing a bool that doesn't contain exactly values 0 or 1
>> or passing an uninitialised pointer.
>> 
>> We have to handle all regular conditions. We don't have to guard against
>> stupidity.
>> 
> 
> I don't think this example has to do with stupidity. To me this just 
> seems like a minimal example of typical integration problems that happen 
> in real world.

If you need to convert between integers and enum values (or one enum and another), and you care about safety of your application (i.e. always), do explicit conversion with an if on the integer (switch on that other enum), and do the error handling at the place where the conversion happens.

> Far fetching I'd talk about distributed services, reading 
> files written by a different program version, and such. Also Qt already 
> deals with such issues where it is obvious:
> 
>     const char * QVariant::typeToName(int typeId) [static]
>     Converts the int representation of the storage type, typeId, to its
>     string representation.
> 
>     Returns a null pointer if the type is QVariant::Invalid(obsolete)
>     or doesn't exist.
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development

-- 
Eike Ziller, Senior Software Engineer - Digia, Qt
 
Digia Germany GmbH, Rudower Chaussee 13, D-12489 Berlin
Geschäftsführer: Mika Pälsi, Juha Varelius, Tuula Haataja
Sitz der Gesellschaft: Berlin, Registergericht: Amtsgericht Charlottenburg, HRB 144331 B




More information about the Development mailing list