Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static_assert does not break compiling immediately

Tags:

Code example:

template <int x>
struct SUM
{
    static_assert(x >= 0, "X must be greater or equal to 0");
    enum {VALUE = x + SUM<x-1>::VALUE};
};

template<>
struct SUM<0>
{
    enum {VALUE = 0};
};

int main()
{
    std::cout << SUM<-1>::VALUE << std::endl;
    return 0;
}

Why compiler does not break compilation on first static_assert but continue work till reaching maximum instantiation depth?

Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/Main.d" -MT"src/Main.d" -o "src/Main.o" "../src/Main.cpp"
../src/Main.cpp: In instantiation of ‘struct SUM<-1>’:
../src/Main.cpp:47:22:   required from here
../src/Main.cpp:26:2: error: static assertion failed: X must be greater or equal to 0
  static_assert(x >= 0, "X must be greater or equal to 0");
  ^

......

../src/Main.cpp: In instantiation of ‘struct SUM<-2>’:
../src/Main.cpp: In instantiation of ‘struct SUM<-900>’:
../src/Main.cpp:27:18:   recursively required from ‘struct SUM<-2>’
../src/Main.cpp:27:18:   required from ‘struct SUM<-1>’
../src/Main.cpp:47:22:   required from here
../src/Main.cpp:26:2: error: static assertion failed: X must be greater or equal to 0
../src/Main.cpp:27:18: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) instantiating ‘struct SUM<-901>’
  enum {VALUE = x + SUM<x-1>::VALUE};
                  ^
../src/Main.cpp:27:18:   recursively required from ‘struct SUM<-2>’
../src/Main.cpp:27:18:   required from ‘struct SUM<-1>’
../src/Main.cpp:47:22:   required from here

../src/Main.cpp:27:18: error: incomplete type ‘SUM<-901>’ used in nested name specifier
make: *** [src/Main.o] Error 1

13:04:05 Build Finished (took 6s.877ms)

The only problem here is that it takes a lot of time till break, and produces a lot of output. Is there a way to make this better? Used compiler: gcc version 4.8.1