1
votes

Create a template function named reversed_binary_value. It must take an arbitrary number of bool values as template parameters. These booleans represent binary digits in reverse order. Your function must return an integer corresponding to the binary value of the digits represented by the booleans. For example: reversed_binary_value<0,0,1>() should return.

This is the problem and I solved in that way.

template<bool...digits>
int reversed_binary_value()
{
    vector<bool> vec = {digits...};
    int result = 0;
    for(int i = 0; i < vec.size(); i++)
        result += pow(2 * vec[i],  i);        
    return result;
}

template <int N>
void sum()
{
    std::cout << "Number is: ";
}

int main()
{
     std::cout << reversed_binary_value<1,0,0,0,1,1>();
    //sum<reversed_binary_value<1,0,0,0,1,1>()>();
}

I'm trying to call function sum but i got this error: call to non-constexpr function 'int reversed_binary_value(). I know for is not compile time instruction. My question is, how can i call function sum ?

2
Why sum has to take its argument through a template value? This is impossible with your current approach, since it would require reversed_binary_value to be able to be evaluated at compile time, but creation of an std::vector makes it impossible as of current standard. - Fureeish
You left out the end of the problem statement. - Lightness Races in Orbit
I know is it impossible to do in that way. I wanna know how can i implement function int reversed_binary_value() to be possible. - Ionut Alexandru
But.. that's literally the assignment. You're supposed to think about it and work it out 😂 Not just get us to give you the solution... - Lightness Races in Orbit
Make it constexpr and don't use any heap allocation, since that's not allowed as of right now in constexpr context. - Fureeish

2 Answers

2
votes

You could use a recursive approach:

#include <iostream>

template <typename = void>
constexpr int binary() {
        return 0;
}
template <bool d, bool...digits>
constexpr int binary() {
        return d + 2 * binary<digits...>();
}

template <int N>
void sum() {
        std::cout << N;
}
int main() {
        sum<binary<1,0,1>()>(); // prints 5
}

This allows you to add constexpr for the binary function (no loops), and allows the constant propagation required for the template call to sum :)

0
votes

The result of reversed_binary_value() cannot be used as a template argument since it is not determined at compile-time (to be that it would need to be a constexpr, but it cannot be that with the current definition). Instead of defining sum() as a template function, you can just make it a regular function taking the integer as argument:

void sum(int N)
{
    std::cout << "Number is: " << N;
}

int main()
{
     std::cout << reversed_binary_value<1,0,0,0,1,1>() << std::endl;
     sum(reversed_binary_value<1,0,0,0,1,1>());
}

One additional thing: instead of depending on the pow() function to calculate the power of 2, you can use bit-shift as follows:

    result += vec[i] * (1u << i);