Consider following trivial example with a simple Test class with operator() which is constexpr. Even if test is not defined with constexpr keyword it may be used to yield size of an std::array during compile time. What is the reason behind such behaviour? Is this well defined in C++?
#include <array> struct Test { constexpr int operator()(int i) const { return i * 2; } }; int main() { Test test{}; std::array<int, test(10)> arr{}; } On the other hand following does not compile:
struct Test { int member; constexpr Test(int member): member(member) {} constexpr int operator()(int i) const { return i * member; } }; int main() { /* constexpr */ Test test{10}; std::array<int, test(10)> arr{}; } In this case we need explicit constexpr in order to compile the code.
testmust be a constant expression to and thus you need a constexpr instance of test. (some slight syntax inprovements will also show intent a bit better on when you mean calling the operator or the constructor) See : godbolt.org/z/KW5E5xhaM (always make your constructors with one argument explicit)constexprkeyword as we also need to have atestinstance in order to invoke the method :)memberat runtime, hence your operator does not always produce a constexpr result, but template arg must be constexpr