Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Inlining Question

Inlining Question

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsswpf
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leslie Sanford
    wrote on last edited by
    #1

    I want to define a set of constants without using macros. I'd like to determine whether these constants are floats or doubles at compile time. So I'm using templates. In my header file, I have this:

    template<bool DoublePrecision>
    struct Constants
    {
    typedef double SampleType;

    static const double pi;
    static const double piDoubled;
    static const double SampleRateDefault;
    static const double DenormalOffset;
    

    };

    template<>
    struct Constants<false>
    {
    typedef float SampleType;

    static const float pi;
    static const float piDoubled;
    static const float SampleRateDefault;
    static const float DenormalOffset;
    

    };

    In my implementation file, I have this:

    #include "Constants.h"

    const double Constants<true>::pi = 3.14159265358979;
    const double Constants<true>::piDoubled = Constants<true>::pi * 2.0;
    const double Constants<true>::SampleRateDefault = 44100.0;
    const double Constants<true>::DenormalOffset = 1.0E-25;

    const float Constants<false>::pi = 3.141593f;
    const float Constants<false>::piDoubled = 6.283185f;
    const float Constants<false>::SampleRateDefault = 44100.0f;
    const float Constants<false>::DenormalOffset = 1.0E-25f;

    All of this compiles fine. My question is whether the values definined here will ultimately be inlined by the compiler where they are used. They are definined in one compilation unit but will be used in other compilation units. Will this prevent them from being inlined? In other words, will this ultimately be less efficient than using macros?

    V N 2 Replies Last reply
    0
    • L Leslie Sanford

      I want to define a set of constants without using macros. I'd like to determine whether these constants are floats or doubles at compile time. So I'm using templates. In my header file, I have this:

      template<bool DoublePrecision>
      struct Constants
      {
      typedef double SampleType;

      static const double pi;
      static const double piDoubled;
      static const double SampleRateDefault;
      static const double DenormalOffset;
      

      };

      template<>
      struct Constants<false>
      {
      typedef float SampleType;

      static const float pi;
      static const float piDoubled;
      static const float SampleRateDefault;
      static const float DenormalOffset;
      

      };

      In my implementation file, I have this:

      #include "Constants.h"

      const double Constants<true>::pi = 3.14159265358979;
      const double Constants<true>::piDoubled = Constants<true>::pi * 2.0;
      const double Constants<true>::SampleRateDefault = 44100.0;
      const double Constants<true>::DenormalOffset = 1.0E-25;

      const float Constants<false>::pi = 3.141593f;
      const float Constants<false>::piDoubled = 6.283185f;
      const float Constants<false>::SampleRateDefault = 44100.0f;
      const float Constants<false>::DenormalOffset = 1.0E-25f;

      All of this compiles fine. My question is whether the values definined here will ultimately be inlined by the compiler where they are used. They are definined in one compilation unit but will be used in other compilation units. Will this prevent them from being inlined? In other words, will this ultimately be less efficient than using macros?

      V Offline
      V Offline
      vikas amin
      wrote on last edited by
      #2

      From design point of view Templates are better then macros ,

      Vikas Amin UNITED STATES STEEL CORPORATION

      My First Article on CP" Virtual Serial Port "[^]

      modified on Thursday, July 24, 2008 5:33 PM

      1 Reply Last reply
      0
      • L Leslie Sanford

        I want to define a set of constants without using macros. I'd like to determine whether these constants are floats or doubles at compile time. So I'm using templates. In my header file, I have this:

        template<bool DoublePrecision>
        struct Constants
        {
        typedef double SampleType;

        static const double pi;
        static const double piDoubled;
        static const double SampleRateDefault;
        static const double DenormalOffset;
        

        };

        template<>
        struct Constants<false>
        {
        typedef float SampleType;

        static const float pi;
        static const float piDoubled;
        static const float SampleRateDefault;
        static const float DenormalOffset;
        

        };

        In my implementation file, I have this:

        #include "Constants.h"

        const double Constants<true>::pi = 3.14159265358979;
        const double Constants<true>::piDoubled = Constants<true>::pi * 2.0;
        const double Constants<true>::SampleRateDefault = 44100.0;
        const double Constants<true>::DenormalOffset = 1.0E-25;

        const float Constants<false>::pi = 3.141593f;
        const float Constants<false>::piDoubled = 6.283185f;
        const float Constants<false>::SampleRateDefault = 44100.0f;
        const float Constants<false>::DenormalOffset = 1.0E-25f;

        All of this compiles fine. My question is whether the values definined here will ultimately be inlined by the compiler where they are used. They are definined in one compilation unit but will be used in other compilation units. Will this prevent them from being inlined? In other words, will this ultimately be less efficient than using macros?

        N Offline
        N Offline
        not_in_use
        wrote on last edited by
        #3

        As far as I understand it they won't be inlined, just like regular constants. I have pretty much the same problem right now, except that I don't need to switch types. In this thread I posted my current idea for a workaround (using operator overloading and inline functions), maybe that could help you. It's a hack, but in theory it should work. As for your templating idea, if you don't need both float and double versions at the same time, you could just use typedefs like so:

        // .h file
        typedef double myFpType; // Changing this to float changes the type of all constants

        const myFpType pi;
        ...

        // .cpp file
        const myFpType pi = myFpType( 3.14159265358979 );
        ...

        or using my workaround idea from the link above:

        typedef double myFpType;

        // In .h file
        INLINE_CONST( myFpType, pi, myFpType( 3.14159265358979 ) );

        But maybe someone knows a better solution that could help us both out. Hope that helps, Peter

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups