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++
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
add a comment |
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
1
how do you inspectnto be larger or equal to1? Is this supposed to be a non type template parameter?
– Guillaume Racicot
Mar 1 at 20:58
@GuillaumeRacicot: My bad, typo. Should've beenint 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
add a comment |
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
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
c++ templates c++17 sfinae template-specialization
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 inspectnto be larger or equal to1? Is this supposed to be a non type template parameter?
– Guillaume Racicot
Mar 1 at 20:58
@GuillaumeRacicot: My bad, typo. Should've beenint 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
add a comment |
1
how do you inspectnto be larger or equal to1? Is this supposed to be a non type template parameter?
– Guillaume Racicot
Mar 1 at 20:58
@GuillaumeRacicot: My bad, typo. Should've beenint 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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 1 at 22:15
BarryBarry
184k21325596
184k21325596
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 6 at 21:48
Always ConfusedAlways Confused
25526
25526
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
how do you inspect
nto be larger or equal to1? 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