Friday, 22 April 2016

Avoid C++ Typo-bugs with this One Cool Trick!


Recently I found an online quiz created by a company which develops some static code analysis tool. You were presented with buggy C++ code which the tool had detected and you had to click on where the bug in the code was.

The large majority of the bugs in the questions were down to typos. These normally occurred when someone had written the same line of code 3 or 4 times, but with different attributes (e.g. .x, .y and .z for a vec3)

Well, here's a neat trick that will allow you to avoid such typos. In the following example I recalculate the min/max values of a bounding box given a vector (pos):

            /* Iterate the X, Y and Z attributes and assign them if necessary */
            auto attrs = { &Vec3::x, &Vec3::y, &Vec3::z };

            for(auto& attr: attrs) {
                if(pos.*attr < bounding_box.min.*attr) bounding_box.min.*attr = pos.*attr;
                if(pos.*attr > bounding_box.max.*attr) bounding_box.max.*attr = pos.*attr;
            }


By using C++11 initializer lists, and auto, you can elegantly create a list of pointers to member attributes. C++11 range-for loops allow you to iterate them easily without a bunch of type definitions, and then you can just apply the pointers to the two vectors. This ensures that you never accidentally check the value of .y against .x or something like that. It's also fewer lines of code than repeating the if checks (4 vs 6). You could also tidy it up more if you create some extra reference variables in the loop. Win.

54 comments:

  1. Thanks for sharing This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality information for free. best resume writing service share and keep on sharing

    ReplyDelete
  2. Nice share, really interesting and can help me deal with the problem on my own now. thanks alot for sharing the post with me i will share it with my friends now..

    ReplyDelete
  3. CrownQQ | Agen Domino QQ | BandarQ | Domino99 Online Terbesar

    Yang Merupakan Agen Bandarq, Domino 99, Dan Bandar Poker Online Terpercaya di asia hadir untuk anda semua dengan permainan permainan menarik dan bonus menarik untuk anda semua

    Bonus yang di berikan CrownQQ :
    * Bonus rollingan 0.5%,setiap minggunya
    * Bonus Refferal 10% + 10%,seumur hidup
    * Bonus Jackpot, yang dapat anda dapatkan dengan mudah

    Games Yang di Hadirkan CrownQQ :
    * Poker Online
    * BandarQ
    * Domino99
    * Bandar Sakong
    * Sakong
    * Bandar66
    * AduQ
    * Sakong

    Info Lebih lanjut Kunjungi :
    Website : AGEN BANDARQ CrownQQ
    BBM : 2B382398
    FB : AgenCrownqq
    Twitter : crown_qq

    ReplyDelete


  4. Cari Situs Judi Online yang fair ?
    No BOT - No ADMIN dan murni PLAYER vs PLAYER
    Solusinya hnya di NAGAQQ AGEN BANDARQ TERPERCAYA
    Bonus Refferal 20%
    Bonus Turn Over 0,5%
    Hanya dengan nominal deposit 15ribu sudah bisa bermain 5 game
    Dan raih jackpot puluhan sampai ratusan juta setiap harinya..
    WHATSAPP : +855967014811
    PIN BB : 2B209F68

    ReplyDelete
  5. Bonus dari Crown99
    Bonus Refferal 20%
    Bonus Turn Over 0,5%
    Ayo gabung sekarang juga hanya dengan
    mengklick Agen BandarQ
    WHATSAPP : +855967646513
    PIN BB : 2B382398

    ReplyDelete
  6. CrownQQ | Agen Domino QQ | BandarQ | Domino99 Online Terbesar
    Mau menang banyak dengan modal sedikit..
    Ayo gabung di Agen Domino Crownqq
    =>Bonus Refferal 20%
    =>Bonus Turn Over 0,5%
    =>Min Deposit Rp20.000
    =>1 User ID 8 Games
    Situs yang bisa memberikan kemenangan Agen BandarQ
    raih kemenangan anda segera di ratudunia.com...
    WHATSAPP : +855967646513
    PIN BB : 2B382398

    ReplyDelete
  7. CROWNQQ I AGEN BANDARQ I BANDARQ ONLINE I ADUQ ONLINE I DOMINOQQ TERBAIK
    Yuk Buruan ikutan bermain di website ratudunia.net
    Sekarang crownqq Memiliki Game terbaru Dan Ternama loh...
    => Bonus Refferal 20%
    => Bonus Turn Over 0,5%
    => Minimal Depo 20.000
    => Minimal WD 20.000
    => 100% Member Asli
    => Pelayanan DP & WD 24 jam
    => Livechat Kami 24 Jam Online
    => Bisa Dimainkan Di Hp Android
    => Di Layani Dengan 5 Bank Terbaik
    => 1 User ID 8 Permainan Menarik
    "NEW AGEN BANDAR 66"
    Ayo gabung sekarang juga hanya dengan
    mengklick Agen BandarQ
    WHATSAPP : +855967646513
    PIN BB : 2B382398

    ReplyDelete
  8. CROWNQQ I AGEN BANDARQ I BANDARQ ONLINE I ADUQ ONLINE I DOMINOQQ TERBAIK
    Yuk Buruan ikutan bermain di website ratudunia.net
    Sekarang crownqq Memiliki Game terbaru Dan Ternama loh...
    => Bonus Refferal 20%
    => Bonus Turn Over 0,5%
    => Minimal Depo 20.000
    => Minimal WD 20.000
    => 100% Member Asli
    => Pelayanan DP & WD 24 jam
    => Livechat Kami 24 Jam Online
    => Bisa Dimainkan Di Hp Android
    => Di Layani Dengan 5 Bank Terbaik
    => 1 User ID 8 Permainan Menarik
    "NEW AGEN BANDAR 66"
    Ayo gabung sekarang juga hanya dengan
    mengklick Agen BandarQ
    WHATSAPP : +855967646513
    PIN BB : 2B382398

    ReplyDelete
  9. Really got a useful blog to read today. Its very informative, keep posting more like this. picbear

    ReplyDelete
  10. Useful stuff. Do you find this article about persuasive essay outline useful?

    ReplyDelete
  11. Dentistry Research Paper Writing Services have come up with Dentistry Writing Services for dentistry coursework writing service students in order for them to score straight A’s in their dentistry paper writing services.

    ReplyDelete
  12. Bagi para penggemar yang ingin bermain Slot Online Deposit Pulsa Tanpa Potongan yang bisa dimainkan dengan sangat mudah dan dimanapun kapanpun kalian inginkan bisa langsung mengunjungi situs IDN89 yang sudah terpercaya melalui https://188.166.252.208/

    ReplyDelete
  13. Multiple inheritances are a function of some item-orientated computer programming languages wherein an item or magnificence can inherit characteristics and features from a couple of parent objects or determine Auto Repair Workshop in Dubai class. This could be addressed in various approaches, along with the usage of virtual inheritance.

    ReplyDelete
  14. This Blog Is What I Used To Be Searching Out. This Piece Of Content Material Will Certainly Assists Me. Thank You For Sharing It. visit my site: Air separation unit suppliers

    ReplyDelete
  15. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!
    고스톱

    ReplyDelete
  16. Thanks for sharing the informative post. If you are looking the Linksys extender setup guidelines . so, we have a best technical expert for handlings your quires. for more information gets touch with us. If you would like to get much from this piece of writing then you have to apply these techniques to your won website.
    스포츠토토

    ReplyDelete
  17. Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post
    한국야동

    ReplyDelete
  18. Thanks for this helpful article. Looking forward to having my portfolio. You can also read some of my great reviews about Best
    This is very useful article. I will connect it back to your site though
    토토사이트

    ReplyDelete
  19. Technologistan is the popoular and most trustworthy resource for technology, telecom, business and auto news in Pakistan
    bmw price in pakistan

    ReplyDelete
  20. It is so interesting I was searching for this topic for a long time. I read this article very carefully and now I got all my best information for writing a blog on uv air sterilizer. I am so glad that I finally find a blog resemblance to my topic. Thanks for this.

    ReplyDelete
  21. This is very Interesting & Informative content, There is also something to share with you guys, Off Track Garage Door Avondale AZ
    you guys try it for best garage services.

    ReplyDelete
  22. You're so interesting! I don’t think I've truly read through a single thing like this before. So wonderful to discover another person with genuine thoughts on this subject. Seriously. 야한동영상

    Also feel free to visit may webpage check this link
    야설

    ReplyDelete
  23. Many thanks for starting this up. This website is something that is required on the web, someone with a bit of originality. 일본야동

    Also feel free to visit may webpage check this link
    한국야동

    ReplyDelete
  24. Thank you for posting the very nice informative article. Looking forward to reading more articles from you. 한국야동닷컴

    Also feel free to visit may webpage check this link
    국산야동

    ReplyDelete
  25. Very good article! We will be linking to this great content on our site. Keep up the good writing 국산야동

    Also feel free to visit may webpage check this link
    야설

    ReplyDelete
  26. What an interesting story! I'm glad I finally found what I was looking for Excellent reading 중국야동넷

    Also feel free to visit may webpage check this link
    야설

    ReplyDelete
  27. Bugs should be taken care just like companies did for RFID injectable transponders to avoid mistakes

    ReplyDelete
  28. "Hello, I am one of the most impressed people in your article. I'm very curious about how you write such a good article.
    CLICK IT: 룰렛사이트탑

    ReplyDelete
  29. Great blog article. Really looking forward to read more. 스포츠토토

    ReplyDelete
  30. It is in reality a nice and useful piece of info. I am glad that you simply shared this useful info with us. Please keep us informed like this. Thanks for sharing. 바둑이사이트넷

    ReplyDelete
  31. So good indeed! Glad to have found your page!! This is such great work!! Interesting to read for sure!! 홀덤

    ReplyDelete
  32. Amazing and Informative Post. Keep Writing and inspiring.
    Insane Growth Hair Oil

    ReplyDelete
  33. Wonderful post. Thank you for such an informative and unique Blog.
    Best Face Wash in Pakistan

    ReplyDelete
  34. Great Post for Beginner to understand. I finally found great post here. Thanks for information. Keep sharing more articles. 파칭코사이트인포

    ReplyDelete
  35. Great Blog. Your article is amazing and inspiring. Keep writing and inspiring.
    Hair Oils

    ReplyDelete
  36. Amazing blog! very useful and informative blog.
    Best Anti-aging Cream in Pakistan

    ReplyDelete
  37. Thank you for this wonderful post, great article, keep up the excellent work. Check out our blog DU LLB Coaching centres

    ReplyDelete
  38. Thanks for sharing excellent informations. Your web-site is very cool. I’m impressed by the details that you have on this site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. I found simply the information I already searched all over the place and simply couldn’t come across. What a great web-site. I’m really impressed about the info you provide in your articles. I must say am highly overwhelmed by your whole story. It’s not easy to get such quality information online nowadays. I look forward to staying here for a long time. I just wanted to say that I love every time visiting your wonderful post! Very powerful and have true and fresh information.Thanks for the post and effort! Please keep sharing more such a blog. 카지노커뮤니티

    ReplyDelete
  39. The next time I read a blog, Hopefully it does not fail me just as much as this particular one. I mean, I know it was my choice to read, but I truly believed you would have something helpful to say. All I hear is a bunch of crying about something that you could fix if you weren’t too busy searching for attention. After I initially commented I seem to have clicked on the -Notify me when new comments are added- checkbox and from now on every time a comment is added I recieve 4 emails with the exact same comment. Is there a way you are able to remove me from that service? An impressive share! I’ve just forwarded this onto a colleague who has been conducting a little homework on this. And he actually ordered me dinner simply because I discovered it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanx for spending time to talk about this topic here on your site. 슈어맨도메인

    ReplyDelete
  40. I really wanted to compose a brief remark to express gratitude to you for all the amazing guidelines you are placing on this site. My rather long internet investigation has now been paid with incredibly good insight to write about with my friends. I ‘d admit that most of us site visitors actually are unequivocally blessed to exist in a useful website with so many brilliant people with very helpful ideas. I feel very happy to have come across your weblog and look forward to so many more cool times reading here. Thanks once more for a lot of things. I have to voice my passion for your kind-heartedness for visitors who absolutely need help on your question. Your special commitment to passing the message up and down appears to be extremely insightful and have in every case allowed associates just like me to realize their pursuits. This helpful guidelines signifies much a person like me and especially to my mates. With thanks; from all of us. 토토핫

    ReplyDelete
  41. Youre so awesome! I don’t intend Ive read anything similar to this prior to. So wonderful to find someone with some original thoughts on this subject. realy thank you for starting this up. this website is something that is required online, a person with a little creativity. useful job for bringing something brand-new to the net! This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging Looking to reading your next post. Terrific article! This is the kind of info that are meant to be shared around the net. Shame on Google for not positioning this post higher! Come on over and visit my website. Nice blog here! Thanks =)| 사설토토

    ReplyDelete
  42. I wish to show thanks to the writer for rescuing me from this challenge. As a result of exploring through the search engines and seeing thoughts which were not powerful, I was thinking my entire life was well over. Existing without the presence of answers to the difficulties you’ve sorted out as a result of your main blog post is a crucial case, as well as ones that might have in a negative way damaged my career if I had not discovered your site. That understanding and kindness in dealing with every aspect was precious. I don’t know what I would have done if I had not come upon such a step like this. I am able to at this time look forward to my future. Thanks for your time very much for your professional and result oriented help. I won’t hesitate to propose your web page to any person who should have guide about this subject matter. Needed to write you that bit of observation to help give thanks the moment again for those splendid opinions you’ve discussed here. It has been so unbelievably generous with people like you to grant easily precisely what many people would have supplied for an ebook to get some profit for themselves, certainly since you could have done it in case you wanted. The solutions additionally served to become a easy way to recognize that some people have the same desire much like my very own to figure out way more with regard to this issue. I’m sure there are lots of more fun occasions ahead for people who view your website. 먹튀검증커뮤니티

    ReplyDelete
  43. I enjoy you because of your whole work on this web site. Gloria really likes engaging in investigation and it’s obvious why. A number of us hear all about the compelling medium you create precious steps via this web site and even strongly encourage contribution from website visitors on that area then our own child is now starting to learn a lot of things. Enjoy the remaining portion of the year. You’re conducting a useful job. I am glad for writing to let you know of the fine experience my friend’s girl had checking your site. She even learned plenty of details, with the inclusion of what it’s like to possess an awesome helping heart to make many others without problems comprehend some impossible things. You truly surpassed people’s expectations. Thanks for producing such helpful, dependable, informative as well as easy tips on that topic to Evelyn. 토토사이트커뮤니티

    ReplyDelete
  44. I just couldn’t leave your website before telling you that I truly enjoyed the top quality info you present to your visitors? Will be back again frequently to check up on new posts. I read this article. I think You put a great deal of exertion to make this article. I like your work. The information you have posted is very useful. The sites you have referred was good. Thanks for sharing 토토사이트

    ReplyDelete
  45. Your post is very helpful to get some effective tips to reduce weight properly. You have shared various nice photos of the same. I would like to thank you for sharing these tips. Surely I will try this at home. Keep updating more simple tips like this. Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I’ll be sure to check back again real soon. 먹튀검증

    ReplyDelete
  46. Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. And I found this one pretty fascinating and it should go into my collection. Very good work! Thanks to your posting, my long search is over. For me, after a long search for this information, your writing has had a very useful impact. I'll bookmark your website. 메이저토토사이트

    ReplyDelete
  47. This article is an appealing wealth of useful informative that is interesting and well-written. I commend your hard work on this and thank you for this information. I know it very well that if anyone visits your blog, then he/she will surely revisit it again. I loved this assignment article and kept updating interesting articles. I will be a regular reader. I am offering help to students over the globe at a low price. I delight in, cause I found just what I was looking for. You have ended my four day long hunt! God Bless you man. Have a great day. 슈어맨

    ReplyDelete
  48. It is my first visit to your blog, and I am very impressed with the articles that you serve. Give adequate knowledge for me. Thank you for sharing useful material. I will be back for the more great post. I want you to thank for your time of this fantastic read!!! I certainly delight in every little of it as well as I have you bookmarked to check out new stuff of your blog a must-read blog site! I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more. 먹튀

    ReplyDelete
  49. After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article. Very useful information shared in this article, nicely written! I will be reading your articles and using the informative tips. Looking forward to read such knowledgeable articles . Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates. 안전놀이터

    ReplyDelete
  50. Wonderful article. It's very useful. As a home tuition service provider, I really appreciate your post. Thanks for sharing this with us. I find a very good website. Great Information sharing .. I am very happy to read this article .. thanks for giving us go through info.Fantastic nice. I appreciate this post. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post. 토토사이트

    ReplyDelete
  51. ambbet เราคือผู้ให้บริการ คาสิโนออนไลน์ ค่ายดังและได้รับความนิยมจากผู้เล่นชาวไทย ไม่ว่าจะเป็น SA Gaming , Sexy Gaming , DreamGaming , AG Asia Gaming และ Pretty Gaming สมัครครั้งเดียวเล่นได้ทุกเกม เล่นง่าย ได้เงินจริง เว็บคาสิโนออนไลน์ ระบบฝากถอน ออโต้ ไม่มีขั้นต่ำ สามารถทำรายการ ฝากถอน ได้ตลอด 24 ชั่วโมง

    ReplyDelete
  52. biogaming เป็นที่นิยมอย่างแพร่หลายไม่ว่าจะเป็นระบบบริการที่ยอดเยี่ยม บริการตลอด 24 ชั่วโมง บริการด้านเกมคาสิโนหลากหลายเช่น บาคาร่าออนไลน์ รูเร็ท แบล็คแจ็ค เสือมังกร ไฮโล และอื่นๆอีกมากมาย ที่มาพร้อมกับโปรโมชั่นโดนใจเน้นๆ ที่นี่ที่ https://biobetgaming.com

    ReplyDelete
  53. betflix เกมสล็อตออนไลน์ ที่จะพาทุกท่านสัมผัสกับ แจ็คพอตแตกง่ายๆ ด้วยเกมที่หลากหลายให้เล่น แนวเกมส์ที่สนุก พร้อมที่จะบริการลูกค้าทุกท่านให้ไปได้ไกลที่สุด แตกล้านจ่ายล้าน แน่นอนมั่นใจที่นี่ที่เดียว betflixsupervip.com

    ReplyDelete