0
template <const long long int lx, const long long int ly, const long long int lz, const long long int cx, const long long int cy, const long long int cz> class rtH{
  public: static const long long int sqlc=cx*cx+cy*cy+cz*cz;
static const long long int ldc=lx*cx+ly*cy+lz*cz;
};
template <const long long int lx, const long long int ly, const long long int lz, const long long int cx, const long long int cy, const long long int cz, const long long int r> class rt{
  public: static const long long int d=rtH<lx,ly,lz,cx,cy,cz>::ldc-sqrt<rtH<lx,ly,lz,cx,cy,cz>::ldc*rtH<lx,ly,lz,cx,cy,cz>::ldc-(rtH<lx,ly,lz,cx,cy,cz>::sqlc-r*r),20>::value
  ;
};
int main(){return rt<1,1,1,1,1,1,1>::d;}

The compiler doesn't complain about instantiating rt, so it knows that lx,ly,lz,cx,cy,cz,r are compile time constants. In rtH, I defined sqlc and ldc as const. These const variables only depend on compile-time constants, so they should be also compile-time constants, right? If so, why does the compiler complains about the parameter for sqrt<> isn't a compile-time constant?

Note: sqrt<> works in other places.

he77789
  • 101
  • 4
  • 2
    Please post a [mre]. – eesiraed Apr 08 '20 at 05:15
  • The variables are not defined as compile time constants. You need to use `constexpr` instead of `const`. Also writing `const long long int` as template parameter is ridiculously superflous. Write either `long long` or `std::int64_t` (or write elsewhere `using std::int64_t` and shorten it to `int64_t`). – ALX23z Apr 08 '20 at 05:31
  • 1
    `const` just means not modifiable by the program, and doesn't imply "compile-time" in any way. `constexpr` is a different keyword that, when applied to a variable, implies compile-time constant. – Zuodian Hu Apr 08 '20 at 05:31
  • @ZuodianHu post that as an answer instead – he77789 Apr 08 '20 at 07:10

2 Answers2

2

const just means not modifiable by the program, and does not imply compile-time in any way. constexpr is a different keyword that, when applied to a variable, implies compile-time constant.

Zuodian Hu
  • 979
  • 4
  • 9
0

It turns out that the problem is not on the compiler's inability; I was overflowing in a part of the expression and the standard explicitly includes signed overflow as a condition for an expression to not be constant.

he77789
  • 101
  • 4