generating functions at compile time
I am trying to generate functions at compile time using boost hana. Here is the code I wrote
#include <boost/hana/transform.hpp>
#include <array>
template<int i>
double f(double x)
return x * i;
int main()
constexpr std::array arr = 1,5,10,100,500;
constexpr auto functions = hana::transform(arr,
(const int a) -> double (*)(double)
return f<a>;
);
when compiling I get the error that f is not convertible to type double (*)(double).
I think the problem is that a is not constexpr(which is not possible since it is a function argument). Is there a way to make this working?
c++ templates metaprogramming
add a comment |
I am trying to generate functions at compile time using boost hana. Here is the code I wrote
#include <boost/hana/transform.hpp>
#include <array>
template<int i>
double f(double x)
return x * i;
int main()
constexpr std::array arr = 1,5,10,100,500;
constexpr auto functions = hana::transform(arr,
(const int a) -> double (*)(double)
return f<a>;
);
when compiling I get the error that f is not convertible to type double (*)(double).
I think the problem is that a is not constexpr(which is not possible since it is a function argument). Is there a way to make this working?
c++ templates metaprogramming
No, there is no way to instantiate a template with a runtime value.
– Galik
Nov 12 '18 at 21:18
but the array is known at compile time...
– user3726947
Nov 12 '18 at 21:22
Well it would be interesting if you wrote aconstexpr
function and used that instead of your lambda. That might work (I don't know how far constexpr is willing to introspect). Aslo ishana::transform
constexpr? Probably not...
– Galik
Nov 12 '18 at 21:46
@galik - "That might work" - Have you tried? If thea
value (the value used forf<a>
) is passed through a template parameter, should works,constexpr
or notconstexpr
. But ifa
is passed as normal parameter, shouldn't works, also inside aconstexpr
function, because aconstexpr
function can be called also run-time.
– max66
Nov 12 '18 at 22:48
Also Lambdas can be constexpr so this makes no diffrence
– user3726947
Nov 13 '18 at 23:35
add a comment |
I am trying to generate functions at compile time using boost hana. Here is the code I wrote
#include <boost/hana/transform.hpp>
#include <array>
template<int i>
double f(double x)
return x * i;
int main()
constexpr std::array arr = 1,5,10,100,500;
constexpr auto functions = hana::transform(arr,
(const int a) -> double (*)(double)
return f<a>;
);
when compiling I get the error that f is not convertible to type double (*)(double).
I think the problem is that a is not constexpr(which is not possible since it is a function argument). Is there a way to make this working?
c++ templates metaprogramming
I am trying to generate functions at compile time using boost hana. Here is the code I wrote
#include <boost/hana/transform.hpp>
#include <array>
template<int i>
double f(double x)
return x * i;
int main()
constexpr std::array arr = 1,5,10,100,500;
constexpr auto functions = hana::transform(arr,
(const int a) -> double (*)(double)
return f<a>;
);
when compiling I get the error that f is not convertible to type double (*)(double).
I think the problem is that a is not constexpr(which is not possible since it is a function argument). Is there a way to make this working?
c++ templates metaprogramming
c++ templates metaprogramming
edited Nov 12 '18 at 21:18
asked Nov 12 '18 at 21:08
user3726947
304210
304210
No, there is no way to instantiate a template with a runtime value.
– Galik
Nov 12 '18 at 21:18
but the array is known at compile time...
– user3726947
Nov 12 '18 at 21:22
Well it would be interesting if you wrote aconstexpr
function and used that instead of your lambda. That might work (I don't know how far constexpr is willing to introspect). Aslo ishana::transform
constexpr? Probably not...
– Galik
Nov 12 '18 at 21:46
@galik - "That might work" - Have you tried? If thea
value (the value used forf<a>
) is passed through a template parameter, should works,constexpr
or notconstexpr
. But ifa
is passed as normal parameter, shouldn't works, also inside aconstexpr
function, because aconstexpr
function can be called also run-time.
– max66
Nov 12 '18 at 22:48
Also Lambdas can be constexpr so this makes no diffrence
– user3726947
Nov 13 '18 at 23:35
add a comment |
No, there is no way to instantiate a template with a runtime value.
– Galik
Nov 12 '18 at 21:18
but the array is known at compile time...
– user3726947
Nov 12 '18 at 21:22
Well it would be interesting if you wrote aconstexpr
function and used that instead of your lambda. That might work (I don't know how far constexpr is willing to introspect). Aslo ishana::transform
constexpr? Probably not...
– Galik
Nov 12 '18 at 21:46
@galik - "That might work" - Have you tried? If thea
value (the value used forf<a>
) is passed through a template parameter, should works,constexpr
or notconstexpr
. But ifa
is passed as normal parameter, shouldn't works, also inside aconstexpr
function, because aconstexpr
function can be called also run-time.
– max66
Nov 12 '18 at 22:48
Also Lambdas can be constexpr so this makes no diffrence
– user3726947
Nov 13 '18 at 23:35
No, there is no way to instantiate a template with a runtime value.
– Galik
Nov 12 '18 at 21:18
No, there is no way to instantiate a template with a runtime value.
– Galik
Nov 12 '18 at 21:18
but the array is known at compile time...
– user3726947
Nov 12 '18 at 21:22
but the array is known at compile time...
– user3726947
Nov 12 '18 at 21:22
Well it would be interesting if you wrote a
constexpr
function and used that instead of your lambda. That might work (I don't know how far constexpr is willing to introspect). Aslo is hana::transform
constexpr? Probably not...– Galik
Nov 12 '18 at 21:46
Well it would be interesting if you wrote a
constexpr
function and used that instead of your lambda. That might work (I don't know how far constexpr is willing to introspect). Aslo is hana::transform
constexpr? Probably not...– Galik
Nov 12 '18 at 21:46
@galik - "That might work" - Have you tried? If the
a
value (the value used for f<a>
) is passed through a template parameter, should works, constexpr
or not constexpr
. But if a
is passed as normal parameter, shouldn't works, also inside a constexpr
function, because a constexpr
function can be called also run-time.– max66
Nov 12 '18 at 22:48
@galik - "That might work" - Have you tried? If the
a
value (the value used for f<a>
) is passed through a template parameter, should works, constexpr
or not constexpr
. But if a
is passed as normal parameter, shouldn't works, also inside a constexpr
function, because a constexpr
function can be called also run-time.– max66
Nov 12 '18 at 22:48
Also Lambdas can be constexpr so this makes no diffrence
– user3726947
Nov 13 '18 at 23:35
Also Lambdas can be constexpr so this makes no diffrence
– user3726947
Nov 13 '18 at 23:35
add a comment |
1 Answer
1
active
oldest
votes
Is there a way to make this working?
Not this way.
Look at your lambda
(const int a) -> double (*)(double)
return f<a>;
You're using the argument a
, that is potentially known run-time in a lambda, as template parameter, that must be known compile type.
Can't work.
so how can I make it work?
– user3726947
Nov 12 '18 at 21:20
1
@user3726947 - I don't see a way withstd::array
. Is good for you if you usestd::integer_sequence
; something asstd::integer_sequence<int, 1, 5, 10, 100, 500>
?
– max66
Nov 12 '18 at 21:22
ok i'll look into that, thanks!
– user3726947
Nov 12 '18 at 21:24
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%2f53270125%2fgenerating-functions-at-compile-time%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is there a way to make this working?
Not this way.
Look at your lambda
(const int a) -> double (*)(double)
return f<a>;
You're using the argument a
, that is potentially known run-time in a lambda, as template parameter, that must be known compile type.
Can't work.
so how can I make it work?
– user3726947
Nov 12 '18 at 21:20
1
@user3726947 - I don't see a way withstd::array
. Is good for you if you usestd::integer_sequence
; something asstd::integer_sequence<int, 1, 5, 10, 100, 500>
?
– max66
Nov 12 '18 at 21:22
ok i'll look into that, thanks!
– user3726947
Nov 12 '18 at 21:24
add a comment |
Is there a way to make this working?
Not this way.
Look at your lambda
(const int a) -> double (*)(double)
return f<a>;
You're using the argument a
, that is potentially known run-time in a lambda, as template parameter, that must be known compile type.
Can't work.
so how can I make it work?
– user3726947
Nov 12 '18 at 21:20
1
@user3726947 - I don't see a way withstd::array
. Is good for you if you usestd::integer_sequence
; something asstd::integer_sequence<int, 1, 5, 10, 100, 500>
?
– max66
Nov 12 '18 at 21:22
ok i'll look into that, thanks!
– user3726947
Nov 12 '18 at 21:24
add a comment |
Is there a way to make this working?
Not this way.
Look at your lambda
(const int a) -> double (*)(double)
return f<a>;
You're using the argument a
, that is potentially known run-time in a lambda, as template parameter, that must be known compile type.
Can't work.
Is there a way to make this working?
Not this way.
Look at your lambda
(const int a) -> double (*)(double)
return f<a>;
You're using the argument a
, that is potentially known run-time in a lambda, as template parameter, that must be known compile type.
Can't work.
answered Nov 12 '18 at 21:19
max66
34.6k63762
34.6k63762
so how can I make it work?
– user3726947
Nov 12 '18 at 21:20
1
@user3726947 - I don't see a way withstd::array
. Is good for you if you usestd::integer_sequence
; something asstd::integer_sequence<int, 1, 5, 10, 100, 500>
?
– max66
Nov 12 '18 at 21:22
ok i'll look into that, thanks!
– user3726947
Nov 12 '18 at 21:24
add a comment |
so how can I make it work?
– user3726947
Nov 12 '18 at 21:20
1
@user3726947 - I don't see a way withstd::array
. Is good for you if you usestd::integer_sequence
; something asstd::integer_sequence<int, 1, 5, 10, 100, 500>
?
– max66
Nov 12 '18 at 21:22
ok i'll look into that, thanks!
– user3726947
Nov 12 '18 at 21:24
so how can I make it work?
– user3726947
Nov 12 '18 at 21:20
so how can I make it work?
– user3726947
Nov 12 '18 at 21:20
1
1
@user3726947 - I don't see a way with
std::array
. Is good for you if you use std::integer_sequence
; something as std::integer_sequence<int, 1, 5, 10, 100, 500>
?– max66
Nov 12 '18 at 21:22
@user3726947 - I don't see a way with
std::array
. Is good for you if you use std::integer_sequence
; something as std::integer_sequence<int, 1, 5, 10, 100, 500>
?– max66
Nov 12 '18 at 21:22
ok i'll look into that, thanks!
– user3726947
Nov 12 '18 at 21:24
ok i'll look into that, thanks!
– user3726947
Nov 12 '18 at 21:24
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53270125%2fgenerating-functions-at-compile-time%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
No, there is no way to instantiate a template with a runtime value.
– Galik
Nov 12 '18 at 21:18
but the array is known at compile time...
– user3726947
Nov 12 '18 at 21:22
Well it would be interesting if you wrote a
constexpr
function and used that instead of your lambda. That might work (I don't know how far constexpr is willing to introspect). Aslo ishana::transform
constexpr? Probably not...– Galik
Nov 12 '18 at 21:46
@galik - "That might work" - Have you tried? If the
a
value (the value used forf<a>
) is passed through a template parameter, should works,constexpr
or notconstexpr
. But ifa
is passed as normal parameter, shouldn't works, also inside aconstexpr
function, because aconstexpr
function can be called also run-time.– max66
Nov 12 '18 at 22:48
Also Lambdas can be constexpr so this makes no diffrence
– user3726947
Nov 13 '18 at 23:35