I present two code snippets, one of them compiles and the other doesn't.
The one that doesn't compile:
class Solution {
public:
const int MAX_NUM = 100;
const int MAX_SIZE = 200;
bool canPartition(vector<int>& nums) {
bitset<(MAX_NUM*MAX_SIZE)/2 + 1> bits(1);
int sum = 0;
for(int num: nums)
{
sum += num;
bits |= bits << num;
}
return !(sum % 2) and bits[sum/2];
}
};
Gives the errors:
error: non-type template argument is not a constant expression
implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function
One that does:
class Solution {
public:
bool canPartition(vector<int>& nums) {
const int MAX_NUM = 100;
const int MAX_SIZE = 200;
bitset<(MAX_NUM*MAX_SIZE)/2 + 1> bits(1);
int sum = 0;
for(int num: nums)
{
sum += num;
bits |= bits << num;
}
return !(sum % 2) and bits[sum/2];
}
};
I read constexpr documentation and found two things which might be issue here:
constexpr must immediately initialized.
It must have constant destruction, i.e. it is not of class type
Could you point out what's the issue here and help me understand the problem?