mercredi 28 novembre 2018

What the benefits by using the template

I'm reading the pbrt and it has defined a type:

template <int nSpectrumSamples>
class CoefficientSpectrum;
class RGBSpectrum : public CoefficientSpectrum<3> {
    using CoefficientSpectrum<3>::c;
typedef RGBSpectrum Spectrum;
// typedef SampledSpectrum Spectrum;

And the author said:

"We have not written the system such that the selection of which Spectrum implementation to use could be resolved at run time; to switch to a different representation, the entire system must be recompiled. One advantage to this design is that many of the various Spectrum methods can be implemented as short functions that can be inlined by the compiler, rather than being left as stand-alone functions that have to be invoked through the relatively slow virtual method call mechanism. Inlining frequently used short functions like these can give a substantial improvement in performance."

1.Why template can inline function but normal way can not?

2.Why do normal way has to use the virtual method?

Linkage to the entire header file: https://github.com/mmp/pbrt-v3/blob/master/src/core/spectrum.h

Aucun commentaire:

Enregistrer un commentaire