[Nfd-dev] code style rule 44

Junxiao Shi shijunxiao at email.arizona.edu
Sat Feb 15 10:34:15 PST 2014


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/de89a503/attachment.html>


More information about the Nfd-dev mailing list