1
template <bool ...T> 
int some_function()
{
  // this is the function with return type int
  // I am not sure how to get the values into the function
}

// this is how I want to call the function
int temp = some_function<1,0,0,0>();

Any suggestion for the function declaration?

  • The code you have shown looks correct. Are you asking how to use the values in `T...`? – NathanOliver Mar 16 '20 at 18:12
  • Yes, that is the question I am asking for. How can I use the values in T...? –  Mar 16 '20 at 18:14
  • 1
    Depends on what you want to do with them. Can you should what you would like your function to do with them? – NathanOliver Mar 16 '20 at 18:15
  • It seems you are looking for https://en.cppreference.com/w/cpp/language/fold basic demonstration is here https://godbolt.org/z/hV4KdZ – calynr Mar 16 '20 at 18:18
  • Those are the binary version of the actual decimal number. I want to reconstruct the decimal number with those binaries. I assume that I can do it when I get those binary numbers somehow! –  Mar 16 '20 at 18:19
  • @arnes but I need the values in T..., is there ant way to get those? I want to play with the numbers in it. –  Mar 16 '20 at 18:28
  • Won't writing `0b1000` for the number be simpler? – StoryTeller - Unslander Monica Mar 16 '20 at 18:28
  • @StoryTeller-UnslanderMonica I must do it with the long way :) –  Mar 16 '20 at 18:31

3 Answers3

4

For your use case, you can use recursion to do what you want. To do that you need two overloads. One with just a single bool parameter, and another with two bool parameters plus the variadic part. That gives you access to each value individually as you recurse your way through the parameter pack. In this case that would look like

// quick and dirty pow fucntion.  There are better ones out there like https://stackoverflow.com/a/101613/4342498
template <typename T, typename U>
auto pow(T base, U exp)
{
    T ret = 1;
    for (int i = 0; i < exp; ++i)
        ret *= base;
    return ret;
}

template <bool Last>
int some_function()
{
    return Last;
}

template <bool First, bool Second, bool... Rest> 
int some_function()
{
    return First * pow(2, sizeof...(Rest) + 1) + some_function<Second, Rest...>();
}

int main()
{
    std::cout << some_function<1,0,0,0>();
}

Which outputs:

8
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
2

Those are the binary version of the actual decimal number. I want to reconstruct the decimal number with those binaries.

While you can perhaps do this more performantly, std::bitset offers a pretty straightforward solution (live example):

template <bool ...T> 
int some_function()
{
    static_assert(sizeof...(T) < sizeof(int) * CHAR_BIT); // We want this to fit in int with no negatives.

    char binary[]{(T + '0')...}; // Put corresponding '0's and '1's into a string.
    std::bitset<sizeof...(T)> bits(binary); // Use the string to make a bitset.
    return bits.to_ulong(); // Convert the bitset to a number.
}
chris
  • 60,560
  • 13
  • 143
  • 205
0

Those are the binary version of the actual decimal number. I want to reconstruct the decimal number with those binaries. I assume that I can do it when I get those binary numbers somehow!

If you can use C++17, so also folding, I suppose you can write something as follows

template <bool ...T> 
int some_function ()
 {
   int ret{};

   return ((ret <<= 1, ret += T), ...);
 }

In C++11 and C++14 is a little less elegant

template <bool ...T> 
int some_function ()
 {
   using unused = int[];

   int ret{};

   (void)unused { 0, (ret <<= 1, ret += T)... };

   return ret;
 }
max66
  • 65,235
  • 10
  • 71
  • 111
  • I have tried both of them and they work pretty well. Thank you very much for your effort. –  Mar 16 '20 at 19:34
  • Since I have already select the solutions above, I cannot select your solution as a correct one. –  Mar 16 '20 at 19:35
  • 1
    @erkus19 - is enough if you find it interesting. – max66 Mar 16 '20 at 20:08