How can I use a constexpr function during pack expansion?

user2296177

I want to do the following:

// have a constexpr function
template<class T>
constexpr T square( T const i )
{
    return i * i;
}

// transform a std::integer_sequence<> by calling the constexpr function on every integer
template<class Fn, class T, T... values>
static constexpr auto make_type( Fn fn, std::integer_sequence<T, values...> )
{
    return std::integer_sequence<T, fn( values )...>{};
}

// so that I can use it like so
using type = decltype( make_type( square, std::integer_sequence<int, 1, 2, 3>{} ) );

However, I get the following error:

...\main.cpp|19|error: 'fn' is not a constant expression|

Columbo

fn is not usable in a constant expression - it's a bog-standard block-scope variable. You have to pass the functor as a type.

template <typename Fn, typename T, T... values>
static constexpr std::integer_sequence<T, Fn{}(values)...>
make_type(std::integer_sequence<T, values...>) {return {};}

And rewrite your function as

struct Square {
    template <typename T> constexpr T operator()(T const& t)
    {return t*t;}
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can I use const & as parameter of a constexpr function?

From Dev

Can I use a fold function to implement pack functionality?

From Dev

Why can't I use constexpr with lambda function?

From Dev

How can I use $variable in a shell brace expansion of a sequence?

From Dev

How can I use command expansion on `\{\}` in `find -exec`?

From Dev

How can I suppress the space between generated arguments during brace expansion?

From Dev

How can I do a runtime assert in a constexpr function?

From Dev

How can I choose a function at compile time based on a constexpr result

From Dev

How can I choose a function at compile time based on a constexpr result

From Dev

Why can not I use constexpr value in function, but I can do the same in scope of this value?

From Dev

Can I use a variable in a Bash brace expansion?

From Dev

Can I use bash brace expansion in for loop?

From Dev

How to use valgrind with a function that is actually the expansion of a macro

From Dev

Python: how can I override a complicated function during unittest?

From Dev

How can I use ChefSpec during refactoring of environments/roles/nodes

From Dev

In Angular 2, how can I use a different route during runtime?

From Dev

How can I do brace expansion on variables?

From Dev

How can I alias a history expansion in zsh?

From Dev

How do I use JFrame.pack()?

From Dev

How can I pack a template class?

From Dev

How can I pack a template class?

From Dev

How can I use indices in the subset function

From Dev

How can I use a function variable in a .then?

From Dev

How can I use an extended jQuery function?

From Dev

How can I use the fromRaw() function of enum

From Dev

How can I use an alias in a function?

From Dev

How can I use function pointers in Nimrod?

From Dev

How can I use splatting in a parameterized function?

From Dev

how can i use a const function with "this"

Related Related

  1. 1

    Can I use const & as parameter of a constexpr function?

  2. 2

    Can I use a fold function to implement pack functionality?

  3. 3

    Why can't I use constexpr with lambda function?

  4. 4

    How can I use $variable in a shell brace expansion of a sequence?

  5. 5

    How can I use command expansion on `\{\}` in `find -exec`?

  6. 6

    How can I suppress the space between generated arguments during brace expansion?

  7. 7

    How can I do a runtime assert in a constexpr function?

  8. 8

    How can I choose a function at compile time based on a constexpr result

  9. 9

    How can I choose a function at compile time based on a constexpr result

  10. 10

    Why can not I use constexpr value in function, but I can do the same in scope of this value?

  11. 11

    Can I use a variable in a Bash brace expansion?

  12. 12

    Can I use bash brace expansion in for loop?

  13. 13

    How to use valgrind with a function that is actually the expansion of a macro

  14. 14

    Python: how can I override a complicated function during unittest?

  15. 15

    How can I use ChefSpec during refactoring of environments/roles/nodes

  16. 16

    In Angular 2, how can I use a different route during runtime?

  17. 17

    How can I do brace expansion on variables?

  18. 18

    How can I alias a history expansion in zsh?

  19. 19

    How do I use JFrame.pack()?

  20. 20

    How can I pack a template class?

  21. 21

    How can I pack a template class?

  22. 22

    How can I use indices in the subset function

  23. 23

    How can I use a function variable in a .then?

  24. 24

    How can I use an extended jQuery function?

  25. 25

    How can I use the fromRaw() function of enum

  26. 26

    How can I use an alias in a function?

  27. 27

    How can I use function pointers in Nimrod?

  28. 28

    How can I use splatting in a parameterized function?

  29. 29

    how can i use a const function with "this"

HotTag

Archive