www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [OT] C++ tips for a D programmer

reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
I am starting in a new job on Monday, in which the primary programming 
language is C++.  For the past four years I've had the privilege of being 
able to use D both professionally and privately, and consequently my C++ 
skills (which were intermediate to begin with) are now a bit rusty.

Since I know there are a lot of extremely talented C++ programmers in the 
D community, I thought I'd ask here for some tips.

Firstly, can you think of any pitfalls to avoid?  In particular, I am 
interested in the ones that stem from subtle differences between the two 
programming languages.  I believe I am already aware of the most obvious 
stuff (such as for example array bounds checking, or lack thereof).

Secondly, can you recommend a good C++ book?  I am looking for one that 
targets an audience with good general programming skills, and which can 
act as a language reference.

Hopefully, my adventures in the world of C++ will give me new 
perspectives on things, allowing me to contribute even better to the 
improvement of D. ;)

-Lars
Aug 31 2011
next sibling parent "Paulo Pinto" <pjmlp progtools.org> writes:
Hi,

assuming that you are not working on a legacy code base, which might bring 
its
own set of quirks.

Use the standard library as much as possible. Later on you can always 
optimize
with help of a profiler.

Avoid C strings, prefer the standard library one.

Don't scatter new/malloc and delete/free everywhere. Keep a proper 
allocation patter,
or better yet, used shared pointers in the code parts where performace hit 
won't matter.

Make good use of forward declarations and PIMPL edioms to speed up 
compilation speed.

Always compile with all warnings enabled, or enabled as error. Disable the 
false positives
in places you are sure what you're doing.

My C++ language reference is always Bjarne's book,
The C++ programming language

but there a few other ones worthwhile reading
Modern C++ Design: Generic Programming and Design Patterns Applied
Effective C++ series
The Design and Evolution of C++
Accelerated C++: Practical Programming by Example
Ruminations on C++: A Decade of Programming Insight and Experience

Good luck on your new job,
Paulo



"Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> wrote in message 
news:j3ksua$2nv2$1 digitalmars.com...
I am starting in a new job on Monday, in which the primary programming
 language is C++.  For the past four years I've had the privilege of being
 able to use D both professionally and privately, and consequently my C++
 skills (which were intermediate to begin with) are now a bit rusty.

 Since I know there are a lot of extremely talented C++ programmers in the
 D community, I thought I'd ask here for some tips.

 Firstly, can you think of any pitfalls to avoid?  In particular, I am
 interested in the ones that stem from subtle differences between the two
 programming languages.  I believe I am already aware of the most obvious
 stuff (such as for example array bounds checking, or lack thereof).

 Secondly, can you recommend a good C++ book?  I am looking for one that
 targets an audience with good general programming skills, and which can
 act as a language reference.

 Hopefully, my adventures in the world of C++ will give me new
 perspectives on things, allowing me to contribute even better to the
 improvement of D. ;)

 -Lars 
Aug 31 2011
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2011-08-31 10:55, Lars T. Kyllingstad wrote:
 I am starting in a new job on Monday, in which the primary programming
 language is C++.  For the past four years I've had the privilege of being
 able to use D both professionally and privately, and consequently my C++
 skills (which were intermediate to begin with) are now a bit rusty.

 Since I know there are a lot of extremely talented C++ programmers in the
 D community, I thought I'd ask here for some tips.

 Firstly, can you think of any pitfalls to avoid?  In particular, I am
 interested in the ones that stem from subtle differences between the two
 programming languages.  I believe I am already aware of the most obvious
 stuff (such as for example array bounds checking, or lack thereof).

 Secondly, can you recommend a good C++ book?  I am looking for one that
 targets an audience with good general programming skills, and which can
 act as a language reference.

 Hopefully, my adventures in the world of C++ will give me new
 perspectives on things, allowing me to contribute even better to the
 improvement of D. ;)

 -Lars
I think Scott Meyers' "Effective C++" is a good book. For references, I would go with Bjarne Stroustrup's "The C++ Programming Language". -- /Jacob Carlborg
Aug 31 2011
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 8/31/11 6:56 AM, Jacob Carlborg wrote:
 On 2011-08-31 10:55, Lars T. Kyllingstad wrote:
 I am starting in a new job on Monday, in which the primary programming
 language is C++. For the past four years I've had the privilege of being
 able to use D both professionally and privately, and consequently my C++
 skills (which were intermediate to begin with) are now a bit rusty.

 Since I know there are a lot of extremely talented C++ programmers in the
 D community, I thought I'd ask here for some tips.

 Firstly, can you think of any pitfalls to avoid? In particular, I am
 interested in the ones that stem from subtle differences between the two
 programming languages. I believe I am already aware of the most obvious
 stuff (such as for example array bounds checking, or lack thereof).

 Secondly, can you recommend a good C++ book? I am looking for one that
 targets an audience with good general programming skills, and which can
 act as a language reference.

 Hopefully, my adventures in the world of C++ will give me new
 perspectives on things, allowing me to contribute even better to the
 improvement of D. ;)

 -Lars
I think Scott Meyers' "Effective C++" is a good book. For references, I would go with Bjarne Stroustrup's "The C++ Programming Language".
Accelerated C++. Andrei
Aug 31 2011
prev sibling next sibling parent reply dsimcha <dsimcha yahoo.com> writes:
On 8/31/2011 4:55 AM, Lars T. Kyllingstad wrote:
 I am starting in a new job on Monday, in which the primary programming
 language is C++.  For the past four years I've had the privilege of being
 able to use D both professionally and privately, and consequently my C++
 skills (which were intermediate to begin with) are now a bit rusty.

 Since I know there are a lot of extremely talented C++ programmers in the
 D community, I thought I'd ask here for some tips.

 Firstly, can you think of any pitfalls to avoid?  In particular, I am
 interested in the ones that stem from subtle differences between the two
 programming languages.  I believe I am already aware of the most obvious
 stuff (such as for example array bounds checking, or lack thereof).

 Secondly, can you recommend a good C++ book?  I am looking for one that
 targets an audience with good general programming skills, and which can
 act as a language reference.

 Hopefully, my adventures in the world of C++ will give me new
 perspectives on things, allowing me to contribute even better to the
 improvement of D. ;)

 -Lars
1. Use C++11 if your boss will let you. You'll feel like there's a lot less missing from it than D. 2. Use Boost if your boss will let you. C++'s standard library is a joke. 3. Pray.
Aug 31 2011
parent Trass3r <un known.com> writes:
Am 31.08.2011, 14:31 Uhr, schrieb dsimcha <dsimcha yahoo.com>:
 2.  Use Boost if your boss will let you.  C++'s standard library is a  
 joke.
Yeah, especially boost foreach and smart pointers save lives (and nerves) :)
Aug 31 2011
prev sibling next sibling parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
On 31-08-2011 10:55, Lars T. Kyllingstad wrote:
 I am starting in a new job on Monday, in which the primary programming
 language is C++.  For the past four years I've had the privilege of being
 able to use D both professionally and privately, and consequently my C++
 skills (which were intermediate to begin with) are now a bit rusty.

 Since I know there are a lot of extremely talented C++ programmers in the
 D community, I thought I'd ask here for some tips.

 Firstly, can you think of any pitfalls to avoid?  In particular, I am
 interested in the ones that stem from subtle differences between the two
 programming languages.  I believe I am already aware of the most obvious
 stuff (such as for example array bounds checking, or lack thereof).

 Secondly, can you recommend a good C++ book?  I am looking for one that
 targets an audience with good general programming skills, and which can
 act as a language reference.

 Hopefully, my adventures in the world of C++ will give me new
 perspectives on things, allowing me to contribute even better to the
 improvement of D. ;)

 -Lars
Whatever you do, avoid multiple inheritance like the plague. It will bite you, especially if you use pointers. - Alex
Aug 31 2011
next sibling parent Simon <s.d.hammett gmail.com> writes:
On 31/08/2011 13:41, Alex Rønne Petersen wrote:
 Whatever you do, avoid multiple inheritance like the plague. It will
 bite you, especially if you use pointers.

 - Alex
I that's a little overly simplistic: Avoid multiple inheritance from classes with actual data members; do use multiple inheritance as a stand in for template mixins. Also as others have mentioned, do use boost. It's a massive time saver, though there's so much in it, it can be a bit hard to find the bit which does what you want. Things to check out specifically: format: http://www.boost.org/doc/libs/1_47_0/libs/format/index.html foreach: http://www.boost.org/doc/libs/1_47_0/doc/html/foreach.html property tree: http://www.boost.org/doc/libs/1_47_0/doc/html/property_tree.html (it's got a small and useful xml library built in) signals: http://www.boost.org/doc/libs/1_47_0/doc/html/signals.html smart_ptr: http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/smart_ptr.htm static_assert: http://www.boost.org/doc/libs/1_47_0/doc/html/boost_staticassert.html string_algorithms: http://www.boost.org/doc/libs/1_47_0/doc/html/string_algo.html Have a poke through those and you'll find they cover a big chunk of D's real time savers. And if you need to do some template programming: mpl: http://www.boost.org/doc/libs/1_47_0/libs/mpl/doc/index.html -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Aug 31 2011
prev sibling parent "Paulo Pinto" <pjmlp progtools.org> writes:
That's a bit too much.

I think at least for mixins and to implement what other languages call 
interfaces it is ok.

For more than that it is better not.

--
Paulo

"Alex Rønne Petersen" <xtzgzorex gmail.com> wrote in message 
news:j3la80$lh4$1 digitalmars.com...
 On 31-08-2011 10:55, Lars T. Kyllingstad wrote:
 I am starting in a new job on Monday, in which the primary programming
 language is C++.  For the past four years I've had the privilege of being
 able to use D both professionally and privately, and consequently my C++
 skills (which were intermediate to begin with) are now a bit rusty.

 Since I know there are a lot of extremely talented C++ programmers in the
 D community, I thought I'd ask here for some tips.

 Firstly, can you think of any pitfalls to avoid?  In particular, I am
 interested in the ones that stem from subtle differences between the two
 programming languages.  I believe I am already aware of the most obvious
 stuff (such as for example array bounds checking, or lack thereof).

 Secondly, can you recommend a good C++ book?  I am looking for one that
 targets an audience with good general programming skills, and which can
 act as a language reference.

 Hopefully, my adventures in the world of C++ will give me new
 perspectives on things, allowing me to contribute even better to the
 improvement of D. ;)

 -Lars
Whatever you do, avoid multiple inheritance like the plague. It will bite you, especially if you use pointers. - Alex
Aug 31 2011
prev sibling next sibling parent Mike Parker <aldacron gmail.com> writes:
On 8/31/2011 5:55 PM, Lars T. Kyllingstad wrote:
 I am starting in a new job on Monday, in which the primary programming
 language is C++.  For the past four years I've had the privilege of being
 able to use D both professionally and privately, and consequently my C++
 skills (which were intermediate to begin with) are now a bit rusty.

 Since I know there are a lot of extremely talented C++ programmers in the
 D community, I thought I'd ask here for some tips.

 Firstly, can you think of any pitfalls to avoid?  In particular, I am
 interested in the ones that stem from subtle differences between the two
 programming languages.  I believe I am already aware of the most obvious
 stuff (such as for example array bounds checking, or lack thereof).

 Secondly, can you recommend a good C++ book?  I am looking for one that
 targets an audience with good general programming skills, and which can
 act as a language reference.

 Hopefully, my adventures in the world of C++ will give me new
 perspectives on things, allowing me to contribute even better to the
 improvement of D. ;)

 -Lars
If you find yourself using the standard library a lot, the book "C++ Standard Library Practical Tips" by Greg Reese should come in handy. I rarely use C++, but this has been quite useful to me for those times when I've stumbled through the C++ muck.
Aug 31 2011
prev sibling parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
On Wed, 31 Aug 2011 08:55:38 +0000, Lars T. Kyllingstad wrote:

 I am starting in a new job on Monday, in which the primary programming
 language is C++.  For the past four years I've had the privilege of
 being able to use D both professionally and privately, and consequently
 my C++ skills (which were intermediate to begin with) are now a bit
 rusty.
 
 Since I know there are a lot of extremely talented C++ programmers in
 the D community, I thought I'd ask here for some tips.
 
 ...
Thanks for all your advice, folks! :) -Lars
Sep 01 2011
parent reply Jose Armando Garcia <jsancio gmail.com> writes:
On Thu, Sep 1, 2011 at 5:57 AM, Lars T. Kyllingstad
<public kyllingen.nospamnet> wrote:
 On Wed, 31 Aug 2011 08:55:38 +0000, Lars T. Kyllingstad wrote:

 I am starting in a new job on Monday, in which the primary programming
 language is C++. =A0For the past four years I've had the privilege of
 being able to use D both professionally and privately, and consequently
 my C++ skills (which were intermediate to begin with) are now a bit
 rusty.

 Since I know there are a lot of extremely talented C++ programmers in
 the D community, I thought I'd ask here for some tips.

 ...
Thanks for all your advice, folks! :)
Most important -- find another job. Just kidding. Good luck. I currently use Scala for my current job. For now, I would probably only code in either Scala or D...
Sep 02 2011
parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
On Fri, 02 Sep 2011 12:09:49 -0700, Jose Armando Garcia wrote:

 On Thu, Sep 1, 2011 at 5:57 AM, Lars T. Kyllingstad
 <public kyllingen.nospamnet> wrote:
 On Wed, 31 Aug 2011 08:55:38 +0000, Lars T. Kyllingstad wrote:

 I am starting in a new job on Monday, in which the primary programming
 language is C++.  For the past four years I've had the privilege of
 being able to use D both professionally and privately, and
 consequently my C++ skills (which were intermediate to begin with) are
 now a bit rusty.

 Since I know there are a lot of extremely talented C++ programmers in
 the D community, I thought I'd ask here for some tips.

 ...
Thanks for all your advice, folks! :)
Most important -- find another job. Just kidding. Good luck.
Hehe. I'm very excited about this one, so I think I'll stick with it. ;) Thanks! -Lars
Sep 03 2011