The generalized gradient approximation (GGA) has been a workhorse exchange-correlation functional for electronic structure studies of extended systems (liquid-phase reactions, solids, heterogeneous and enzymatic catalysis, biopolymers) because its dependence on only the spin-labeled electron densities and their reduced gradients makes it the most affordable choice that produces realistic results for thermochemistry. However, much recent research has focused on its poor performance for solid-state lattice constants; the results for lattice constants can be improved but only at the cost of making the energetic predictions worse. In the present article, we propose a new density functional, called N12, which may be thought of as a generalization of range-separated functionals. The N12 functional depends only on the spin-labeled electron densities and their reduced gradients, but with a new kind of nonseparable term that gives it much greater flexibility. The N12 functional is the first exchange-correlation functional depending only on the spin-labeled electron densities and their reduced gradients that simultaneously provides good accuracy for the four key energetic and structural properties of solids and molecules, namely, solid-state cohesive energies and lattice constants and molecular atomization energies and bond lengths.