[Assorted-commits] SF.net SVN: assorted:[1214] sandbox/trunk/src/cc/conversions.cc
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-02-21 23:55:58
|
Revision: 1214 http://assorted.svn.sourceforge.net/assorted/?rev=1214&view=rev Author: yangzhang Date: 2009-02-21 23:55:46 +0000 (Sat, 21 Feb 2009) Log Message: ----------- updated conversions with -Wconversion demo Modified Paths: -------------- sandbox/trunk/src/cc/conversions.cc Modified: sandbox/trunk/src/cc/conversions.cc =================================================================== --- sandbox/trunk/src/cc/conversions.cc 2009-02-21 09:01:45 UTC (rev 1213) +++ sandbox/trunk/src/cc/conversions.cc 2009-02-21 23:55:46 UTC (rev 1214) @@ -1,6 +1,7 @@ // Demo of conversions. #include <iostream> +#include <cstdlib> #include <string> using namespace std; @@ -34,5 +35,16 @@ // C++ decides the "best" function to call by ranking all the possible calls. Standard conversions rank better than user-defined conversions, so #1 is called. f(string("hello")); f("hello"); + + long long l = 0; + // Enable -Wconversion, and you'll get: + // + // warning: conversion to ‘double’ from ‘long long int’ may alter its value + // + // Must explicitly convert the latter l with double(l). + // + // double d = double(l) * 1000 / l; + double d = double(l) * 1000 / double(l); + cout << d << endl; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |