[Nfd-dev] code style rule 44

Alex Afanasyev alexander.afanasyev at ucla.edu
Sat Feb 15 19:52:41 PST 2014


This rule is very strange and I don't like following it.  Implicit conversions generally a good thing.  For the cases implicit conversion is undesirable, constructors should be mark explicit.

I don't see problems with any of the listed examples, except with enum (some compiler may issue warnings).

I prefer not considering this rule for us.

---
Alex


On Feb 15, 2014, at 10:34 AM, Junxiao Shi <shijunxiao at email.arizona.edu> wrote:

> Hi Alex
> 
> I need clarification on code guidelines rule 44:
> 44. (C, C++, C# and Java only) Type conversions must always be done explicitly. Never rely on implicit type conversion.
> floatValue = (float)intValue; // NOT: floatValue = intValue;
> By this, the programmer indicates that he is aware of the different types involved and that the mix is intentional.
> 
> 
> Which of the following are allowed?
> 
> //01
> std::string x = "example"; // char[] is implicitly converted to std::string
> 
> //02
> uint64_t n = 1 + 1; // int is implicitly converted to uint64_t
> 
> //03
> uint64_t a = static_cast<uint64_t>(1);
> uint32_t b = static_cast<uint32_t>(2);
> if (a == b) { // uint32_t is implicitly converted to uint64_t
> }
> 
> //04
> void func(std::string x);
> func("example"); // char[] is implicitly converted to std::string
> 
> //05
> uint8_t x = tlv::Interest; // enum is implicitly converted to uint8_t
> 
> //06
> int x = 1;
> float y = x;
> 
> 
> My opinion is to allow:
> numeric literals and enum members to any numeric type
> allows 02 05 above
> implicit conversion from T[] to std::basic_string<T>
> allows 01 04 above
> and disallow:
> implicit conversion between numeric types
> disallows 03 06 above
> use of implicit constructors (in most cases)
> This also means constructor taking one argument should be marked explicit.
> use of implicit conversion operators (in most cases)
> 
> Yours, Junxiao

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.lists.cs.ucla.edu/pipermail/nfd-dev/attachments/20140215/abe5a39d/attachment.html>


More information about the Nfd-dev mailing list