The code compiles and runs, but I’m trying to understand how the expression (obj.*funcPtr)(12)
can be evaluated at compile-time when obj
is not declared as constexpr
. I would expect that this might not meet the standard requirements for compile-time evaluation.
#include <iostream>
#include <stdexcept>
template <typename T, size_t N>
class Array
{
public:
T& operator[](size_t i)
{
if (i>=N)
{
throw std::out_of_range("Bd index!");
}
return data_[i];
}
constexpr size_t Size() const
{
return N;
}
private:
T data_[N]{};
};
class MyClass
{
public:
constexpr int myFunction(int value)
{
return value*2;
}
};
int main()
{
constexpr int (MyClass::*funcPtr)(int) = &MyClass::myFunction;
MyClass obj;
Array<int , (obj.*funcPtr)(12) > arr;
std::cout << arr.Size() << 'n';
}