Add member functions and member variables based on template argument2019 Community Moderator ElectionStoring C++ template function definitions in a .CPP fileFunction passed as template argumentStatic array of const pointers to overloaded, templatized member functionCan a C++ class member function template be virtual?Default template arguments for function templatesFunction templates and Private Inheritancestd::async call of member functionClass specialization based on the template argument's template argumentC++ member function of class template not recognizedCall to base member function template ambiguity in C++

If "dar" means "to give", what does "daros" mean?

How can my new character avoid being a role-playing handicap to the party?

How is the partial sum of a geometric sequence calculated?

Could Sinn Fein swing any Brexit vote in Parliament?

Can you move over difficult terrain with only 5 feet of movement?

What is the relationship between relativity and the Doppler effect?

Wrapping homogeneous Python objects

How to define limit operations in general topological spaces? Are nets able to do this?

Writing in a Christian voice

Synchronized implementation of a bank account in Java

Do US professors/group leaders only get a salary, but no group budget?

What does "Four-F." mean?

Maths symbols and unicode-math input inside siunitx commands

What favor did Moody owe Dumbledore?

Violin - Can double stops be played when the strings are not next to each other?

How could an airship be repaired midflight?

Probably overheated black color SMD pads

Hausdorff dimension of the boundary of fibres of Lipschitz maps

Does the attack bonus from a Masterwork weapon stack with the attack bonus from Masterwork ammunition?

Pronounciation of the combination "st" in spanish accents

The average age of first marriage in Russia

What does Deadpool mean by "left the house in that shirt"?

How can an organ that provides biological immortality be unable to regenerate?

What is the significance behind "40 days" that often appears in the Bible?



Add member functions and member variables based on template argument



2019 Community Moderator ElectionStoring C++ template function definitions in a .CPP fileFunction passed as template argumentStatic array of const pointers to overloaded, templatized member functionCan a C++ class member function template be virtual?Default template arguments for function templatesFunction templates and Private Inheritancestd::async call of member functionClass specialization based on the template argument's template argumentC++ member function of class template not recognizedCall to base member function template ambiguity in C++










6















I have a family of functions f_n where f_0 is continuous, f_1 is continuously differentiable, $f_n in C^n[a,b]$ so on. I have a C++ class which gives a numerical evaluation of f_n via a lookup table on a vector v



template<int n, typename Real=double>
class f

public:
f() /* initialize v */

Real operator()(Real x) /* find appropriate index for x, and interpolate */

private:
std::vector<Real> v;
;


However, if f is differentiable (n >= 1), I want to add a member function:



template<int n, typename Real=double>
class f

public:
f() /* initialize v and dv */

Real operator()(Real x) /* find appropriate index for x, and interpolate on v */

Real prime(Real x) /* find appropriate index for x, and interpolate on dv */

private:
std::vector<Real> v;
std::vector<Real> dv;
;


I would also like to add a second derivative member for n >= 2, so on.
Can this be done in a single class? (C++17 syntax is acceptable for me.)










share|improve this question



















  • 1





    how do you inspect n to be larger or equal to 1? Is this supposed to be a non type template parameter?

    – Guillaume Racicot
    Mar 1 at 20:58











  • @GuillaumeRacicot: My bad, typo. Should've been int n.

    – user14717
    Mar 1 at 21:16











  • "Can this be done in a single class?" Well, no. You have a different class for each pair of template parameters. So inherently more than a single class. Did you mean a single (template) declaration?

    – JaMiT
    Mar 1 at 23:37















6















I have a family of functions f_n where f_0 is continuous, f_1 is continuously differentiable, $f_n in C^n[a,b]$ so on. I have a C++ class which gives a numerical evaluation of f_n via a lookup table on a vector v



template<int n, typename Real=double>
class f

public:
f() /* initialize v */

Real operator()(Real x) /* find appropriate index for x, and interpolate */

private:
std::vector<Real> v;
;


However, if f is differentiable (n >= 1), I want to add a member function:



template<int n, typename Real=double>
class f

public:
f() /* initialize v and dv */

Real operator()(Real x) /* find appropriate index for x, and interpolate on v */

Real prime(Real x) /* find appropriate index for x, and interpolate on dv */

private:
std::vector<Real> v;
std::vector<Real> dv;
;


I would also like to add a second derivative member for n >= 2, so on.
Can this be done in a single class? (C++17 syntax is acceptable for me.)










share|improve this question



















  • 1





    how do you inspect n to be larger or equal to 1? Is this supposed to be a non type template parameter?

    – Guillaume Racicot
    Mar 1 at 20:58











  • @GuillaumeRacicot: My bad, typo. Should've been int n.

    – user14717
    Mar 1 at 21:16











  • "Can this be done in a single class?" Well, no. You have a different class for each pair of template parameters. So inherently more than a single class. Did you mean a single (template) declaration?

    – JaMiT
    Mar 1 at 23:37













6












6








6


1






I have a family of functions f_n where f_0 is continuous, f_1 is continuously differentiable, $f_n in C^n[a,b]$ so on. I have a C++ class which gives a numerical evaluation of f_n via a lookup table on a vector v



template<int n, typename Real=double>
class f

public:
f() /* initialize v */

Real operator()(Real x) /* find appropriate index for x, and interpolate */

private:
std::vector<Real> v;
;


However, if f is differentiable (n >= 1), I want to add a member function:



template<int n, typename Real=double>
class f

public:
f() /* initialize v and dv */

Real operator()(Real x) /* find appropriate index for x, and interpolate on v */

Real prime(Real x) /* find appropriate index for x, and interpolate on dv */

private:
std::vector<Real> v;
std::vector<Real> dv;
;


I would also like to add a second derivative member for n >= 2, so on.
Can this be done in a single class? (C++17 syntax is acceptable for me.)










share|improve this question
















I have a family of functions f_n where f_0 is continuous, f_1 is continuously differentiable, $f_n in C^n[a,b]$ so on. I have a C++ class which gives a numerical evaluation of f_n via a lookup table on a vector v



template<int n, typename Real=double>
class f

public:
f() /* initialize v */

Real operator()(Real x) /* find appropriate index for x, and interpolate */

private:
std::vector<Real> v;
;


However, if f is differentiable (n >= 1), I want to add a member function:



template<int n, typename Real=double>
class f

public:
f() /* initialize v and dv */

Real operator()(Real x) /* find appropriate index for x, and interpolate on v */

Real prime(Real x) /* find appropriate index for x, and interpolate on dv */

private:
std::vector<Real> v;
std::vector<Real> dv;
;


I would also like to add a second derivative member for n >= 2, so on.
Can this be done in a single class? (C++17 syntax is acceptable for me.)







c++ templates c++17 sfinae template-specialization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 1 at 21:19









max66

37.9k74471




37.9k74471










asked Mar 1 at 20:53









user14717user14717

1,67621940




1,67621940







  • 1





    how do you inspect n to be larger or equal to 1? Is this supposed to be a non type template parameter?

    – Guillaume Racicot
    Mar 1 at 20:58











  • @GuillaumeRacicot: My bad, typo. Should've been int n.

    – user14717
    Mar 1 at 21:16











  • "Can this be done in a single class?" Well, no. You have a different class for each pair of template parameters. So inherently more than a single class. Did you mean a single (template) declaration?

    – JaMiT
    Mar 1 at 23:37












  • 1





    how do you inspect n to be larger or equal to 1? Is this supposed to be a non type template parameter?

    – Guillaume Racicot
    Mar 1 at 20:58











  • @GuillaumeRacicot: My bad, typo. Should've been int n.

    – user14717
    Mar 1 at 21:16











  • "Can this be done in a single class?" Well, no. You have a different class for each pair of template parameters. So inherently more than a single class. Did you mean a single (template) declaration?

    – JaMiT
    Mar 1 at 23:37







1




1





how do you inspect n to be larger or equal to 1? Is this supposed to be a non type template parameter?

– Guillaume Racicot
Mar 1 at 20:58





how do you inspect n to be larger or equal to 1? Is this supposed to be a non type template parameter?

– Guillaume Racicot
Mar 1 at 20:58













@GuillaumeRacicot: My bad, typo. Should've been int n.

– user14717
Mar 1 at 21:16





@GuillaumeRacicot: My bad, typo. Should've been int n.

– user14717
Mar 1 at 21:16













"Can this be done in a single class?" Well, no. You have a different class for each pair of template parameters. So inherently more than a single class. Did you mean a single (template) declaration?

– JaMiT
Mar 1 at 23:37





"Can this be done in a single class?" Well, no. You have a different class for each pair of template parameters. So inherently more than a single class. Did you mean a single (template) declaration?

– JaMiT
Mar 1 at 23:37












2 Answers
2






active

oldest

votes


















4














For each n > 0, we add a new member function taking that value as an argument that inherits from the next level down:



template<int n, typename Real=double>
class f
: public f<n-1, Real>

public:
f() /* initialize dv */

using f<n-1, Real>::prime;
Real prime(Real x, integral_constant<int, n>)
/* find appropriate index for x, and interpolate on dv */


protected:
std::vector<Real> dv;
;


Where the base version adds the operator():



template<typename Real=double>
class f<0, Real>

public:
f() /* initialize v */

Real operator()(Real x) /* find appropriate index for x, and interpolate */
Real prime(Real x) return (*this)(x);

protected:
std::vector<Real> v;
;


This means that the first derivative calls prime(x, integral_constant<int, 1>), the second derivative calls prime(x, integral_constant<int, 2>), etc.






share|improve this answer






























    1














    You can simply have a template member function and a static_assert that ensures you are not taking a derivative that is not supported by your class. Eg:



    template <int n, /* other stuff */>
    class f

    /* Other stuff not shown */
    template <int p>
    Real prime(Real x)

    static_assert(p <= n, "unsupported derivative");
    /* do whatever you need to to implement the pth derivative */

    ;


    Thus an object of type f<1> will support prime<1>() but not prime<2>(), etc. If you accidentally call prime<3> on an object of type f<1> the compiler will call you out on it. Up to you whether you want to think of prime<0> as identical to operator () or change your static_assert to include a check for p > 0.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54952158%2fadd-member-functions-and-member-variables-based-on-template-argument%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      For each n > 0, we add a new member function taking that value as an argument that inherits from the next level down:



      template<int n, typename Real=double>
      class f
      : public f<n-1, Real>

      public:
      f() /* initialize dv */

      using f<n-1, Real>::prime;
      Real prime(Real x, integral_constant<int, n>)
      /* find appropriate index for x, and interpolate on dv */


      protected:
      std::vector<Real> dv;
      ;


      Where the base version adds the operator():



      template<typename Real=double>
      class f<0, Real>

      public:
      f() /* initialize v */

      Real operator()(Real x) /* find appropriate index for x, and interpolate */
      Real prime(Real x) return (*this)(x);

      protected:
      std::vector<Real> v;
      ;


      This means that the first derivative calls prime(x, integral_constant<int, 1>), the second derivative calls prime(x, integral_constant<int, 2>), etc.






      share|improve this answer



























        4














        For each n > 0, we add a new member function taking that value as an argument that inherits from the next level down:



        template<int n, typename Real=double>
        class f
        : public f<n-1, Real>

        public:
        f() /* initialize dv */

        using f<n-1, Real>::prime;
        Real prime(Real x, integral_constant<int, n>)
        /* find appropriate index for x, and interpolate on dv */


        protected:
        std::vector<Real> dv;
        ;


        Where the base version adds the operator():



        template<typename Real=double>
        class f<0, Real>

        public:
        f() /* initialize v */

        Real operator()(Real x) /* find appropriate index for x, and interpolate */
        Real prime(Real x) return (*this)(x);

        protected:
        std::vector<Real> v;
        ;


        This means that the first derivative calls prime(x, integral_constant<int, 1>), the second derivative calls prime(x, integral_constant<int, 2>), etc.






        share|improve this answer

























          4












          4








          4







          For each n > 0, we add a new member function taking that value as an argument that inherits from the next level down:



          template<int n, typename Real=double>
          class f
          : public f<n-1, Real>

          public:
          f() /* initialize dv */

          using f<n-1, Real>::prime;
          Real prime(Real x, integral_constant<int, n>)
          /* find appropriate index for x, and interpolate on dv */


          protected:
          std::vector<Real> dv;
          ;


          Where the base version adds the operator():



          template<typename Real=double>
          class f<0, Real>

          public:
          f() /* initialize v */

          Real operator()(Real x) /* find appropriate index for x, and interpolate */
          Real prime(Real x) return (*this)(x);

          protected:
          std::vector<Real> v;
          ;


          This means that the first derivative calls prime(x, integral_constant<int, 1>), the second derivative calls prime(x, integral_constant<int, 2>), etc.






          share|improve this answer













          For each n > 0, we add a new member function taking that value as an argument that inherits from the next level down:



          template<int n, typename Real=double>
          class f
          : public f<n-1, Real>

          public:
          f() /* initialize dv */

          using f<n-1, Real>::prime;
          Real prime(Real x, integral_constant<int, n>)
          /* find appropriate index for x, and interpolate on dv */


          protected:
          std::vector<Real> dv;
          ;


          Where the base version adds the operator():



          template<typename Real=double>
          class f<0, Real>

          public:
          f() /* initialize v */

          Real operator()(Real x) /* find appropriate index for x, and interpolate */
          Real prime(Real x) return (*this)(x);

          protected:
          std::vector<Real> v;
          ;


          This means that the first derivative calls prime(x, integral_constant<int, 1>), the second derivative calls prime(x, integral_constant<int, 2>), etc.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 1 at 22:15









          BarryBarry

          184k21325596




          184k21325596























              1














              You can simply have a template member function and a static_assert that ensures you are not taking a derivative that is not supported by your class. Eg:



              template <int n, /* other stuff */>
              class f

              /* Other stuff not shown */
              template <int p>
              Real prime(Real x)

              static_assert(p <= n, "unsupported derivative");
              /* do whatever you need to to implement the pth derivative */

              ;


              Thus an object of type f<1> will support prime<1>() but not prime<2>(), etc. If you accidentally call prime<3> on an object of type f<1> the compiler will call you out on it. Up to you whether you want to think of prime<0> as identical to operator () or change your static_assert to include a check for p > 0.






              share|improve this answer



























                1














                You can simply have a template member function and a static_assert that ensures you are not taking a derivative that is not supported by your class. Eg:



                template <int n, /* other stuff */>
                class f

                /* Other stuff not shown */
                template <int p>
                Real prime(Real x)

                static_assert(p <= n, "unsupported derivative");
                /* do whatever you need to to implement the pth derivative */

                ;


                Thus an object of type f<1> will support prime<1>() but not prime<2>(), etc. If you accidentally call prime<3> on an object of type f<1> the compiler will call you out on it. Up to you whether you want to think of prime<0> as identical to operator () or change your static_assert to include a check for p > 0.






                share|improve this answer

























                  1












                  1








                  1







                  You can simply have a template member function and a static_assert that ensures you are not taking a derivative that is not supported by your class. Eg:



                  template <int n, /* other stuff */>
                  class f

                  /* Other stuff not shown */
                  template <int p>
                  Real prime(Real x)

                  static_assert(p <= n, "unsupported derivative");
                  /* do whatever you need to to implement the pth derivative */

                  ;


                  Thus an object of type f<1> will support prime<1>() but not prime<2>(), etc. If you accidentally call prime<3> on an object of type f<1> the compiler will call you out on it. Up to you whether you want to think of prime<0> as identical to operator () or change your static_assert to include a check for p > 0.






                  share|improve this answer













                  You can simply have a template member function and a static_assert that ensures you are not taking a derivative that is not supported by your class. Eg:



                  template <int n, /* other stuff */>
                  class f

                  /* Other stuff not shown */
                  template <int p>
                  Real prime(Real x)

                  static_assert(p <= n, "unsupported derivative");
                  /* do whatever you need to to implement the pth derivative */

                  ;


                  Thus an object of type f<1> will support prime<1>() but not prime<2>(), etc. If you accidentally call prime<3> on an object of type f<1> the compiler will call you out on it. Up to you whether you want to think of prime<0> as identical to operator () or change your static_assert to include a check for p > 0.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 6 at 21:48









                  Always ConfusedAlways Confused

                  25526




                  25526



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54952158%2fadd-member-functions-and-member-variables-based-on-template-argument%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

                      Алба-Юлія

                      Захаров Федір Захарович