I know that non-type template argument for intgral type must be const expression so:
template <int E>
class cat
{
public:
int array[E];
};
int main()
{
cat<4> ob; // ??
}
From what I’ve read only const variables that get initialized with const expressions are const expressions. In this example, we have int E = 4;, so E will not be a constexpression.
So why doesn’t cat<4> ob; throw an error? Am I missing something here?
And how will int array[E]; be created if E is not known at compile time?
Eis4before actual compilation starts. Template specialization takes place before that, which means that the code actually seen by the compiler is something likeThis is a fairly loose interpretation, don’t take it ad-litteram.
To really test this scenario out, you can try: