The value of the `defValue 'patterns cannot appear in a constant expression

As I understand it, the meaning of the template should be known at compile time. so I am writing a small example, just to see that I get it, but by the way, I did not. so I get the following:

`defValue' cannot appear in a constant-
 expression

Can anyone ask what the problem is and how to fix it?

#include <iostream>
template <class T,T defaultVal, int dim=255>
class Vec
{
    T _vec[dim];
    int _dim;
    public:
    Vec () : _dim(dim)
    {
       for (int i=0;i<_dim;++i)
       {
         _vec[i] = defaultVal;
       }
    }
    ~Vec () {};
// other operators and stuff
};

int main ()
{
    int defValue = 0;
    Vec < int,defValue > vecWithDefVal;// here is the problem but i don't know why
}
+3
source share
2 answers

C ++ 03 14.3.2

Non-type Argument Pattern

The template argument for a non-piggy template without template must be one of the following:

- integral constant expression of an integral or enumerated type; or

- name of the asymmetric pattern; or

- , template-ids, , id-expression, a , , - ; - , 5.3.1.

defValue [ -] , .

int defValue = 0; const int defValue = 0, [. ].

Vec <int,0> vecWithDefVal;

+1

. , Vec < int,0 > vecWithDefVal; .

+1

Source: https://habr.com/ru/post/1767653/


All Articles