generating functions at compile time












0














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?










share|improve this question
























  • 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 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










  • Also Lambdas can be constexpr so this makes no diffrence
    – user3726947
    Nov 13 '18 at 23:35


















0














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?










share|improve this question
























  • 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 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










  • Also Lambdas can be constexpr so this makes no diffrence
    – user3726947
    Nov 13 '18 at 23:35
















0












0








0


1





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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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










  • 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












  • 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












  • @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


















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














1 Answer
1






active

oldest

votes


















-1















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.






share|improve this answer





















  • so how can I make it work?
    – user3726947
    Nov 12 '18 at 21:20








  • 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










  • ok i'll look into that, thanks!
    – user3726947
    Nov 12 '18 at 21:24











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%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









-1















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.






share|improve this answer





















  • so how can I make it work?
    – user3726947
    Nov 12 '18 at 21:20








  • 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










  • ok i'll look into that, thanks!
    – user3726947
    Nov 12 '18 at 21:24
















-1















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.






share|improve this answer





















  • so how can I make it work?
    – user3726947
    Nov 12 '18 at 21:20








  • 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










  • ok i'll look into that, thanks!
    – user3726947
    Nov 12 '18 at 21:24














-1












-1








-1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 21:19









max66

34.4k63762




34.4k63762












  • so how can I make it work?
    – user3726947
    Nov 12 '18 at 21:20








  • 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










  • 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








  • 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










  • 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


















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.





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.




draft saved


draft discarded














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





















































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

Bressuire

Vorschmack

Quarantine