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 ?
sumhas to take its argument through atemplatevalue? This is impossible with your current approach, since it would requirereversed_binary_valueto be able to be evaluated at compile time, but creation of anstd::vectormakes it impossible as of current standard. - Fureeishint reversed_binary_value()to be possible. - Ionut Alexandruconstexprand don't use any heap allocation, since that's not allowed as of right now inconstexprcontext. - Fureeish