It might be worth reading about the hyperfocal distance if you don't already know about it. For 40mm f2.8 on FF the hyperfocal distance is 18.9m, if you focus at this point everything from 9.5m to infinite will be in focus.
Depth of field and hyperfocal distance calculator
www.dofmaster.com
As Lee says it's not always necessary to stop a lens down for landscape, depending on what your requirements are of course. I do tend to stop mine down for a couple of reasons, my go to landscape lens is sharper across the frame stopped down, and I tend to want as much DOF as possible. However, there are times such as Lee's photo where having the foreground out of focus works really well.
I tend to stop my lens down to f8 or f11 as diffraction starts to creep in at smaller apertures, I then use the hyperfocal distance to get as much DOF as possible. A more oldschool way, which many still use today, is to stop down to f22 and focus on the main subject (the bridge in the example above), f22 should still give a large DOF despite focussing quite a way into the frame. The problem with this, and why I don't do it, is diffraction is quite noticeable at f22. The only time I shoot smaller than f11 is if I want the starburst/sunburst effect from lights/the sun.