Why does the VC2012 compiler get into stackoverflow (bug C1063)?

I had my own working iterator to move the pixels in the image horizontally or vertically. The template parameter can be const or not (a neat trick from Dr. Dobb's ).

Then I realized that there is a std::iterator base class, and I thought that I would make my stuff more STLly and inherit it.

Unfortunately, now Visual Studio 2012 (version 11.0.60315.01 Update 2) will no longer compile it. I really managed to get the stackoverflow compiler. This post is:

Error 1 error C1063: compiler limit: compiler stack overflow d: \ ... \ source.cpp 42 1 ConsoleApplication3

A very stripped down version of my class looks like this:

 #include <iterator> // This comes from the outer image class. typedef float color_type; template<bool IS_CONST = false> struct Iterator : public std::iterator<std::random_access_iterator_tag, color_type> { // This is myself... typedef Iterator<IS_CONST> iterator; // ... and my variants. typedef Iterator<true> const_iterator; typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; // Make base class typedefs available. typedef typename iterator::value_type value_type; typedef typename iterator::difference_type difference_type; // My own typedefs to deal with immutability. typedef value_type const & const_reference; typedef typename std::conditional<IS_CONST, const_reference, typename iterator::reference>::type reference; Iterator() : _position(nullptr), _step() { } Iterator(value_type * const position, difference_type const step) : _position(position), _step(step) { } iterator const operator-(difference_type n) const { return iterator(*this) -= n; } difference_type operator-(iterator const rhs) const { assert(_step == rhs._step); return (_position - rhs._position) / _step; } protected: value_type * _position; difference_type _step; }; int main() { float a = 3.141f; // Instanciate all variants. Iterator<false> empty; Iterator<true> const_empty; Iterator<false> some(&a, 5); Iterator<true> const_some(&a, 5); return 0; } 

Removing one of the two operator- makes the compiler happy.

Can someone explain to me what the problem is? Or is it even better to fix it?

thanks


Update: Oh, by the way, GCC 4.7.2 happily compiles it .

+4
source share
2 answers

Oh my, that was stupid ... Playing with jerry answer , I found an error. Of course, the compiler gets into recursion, which leads to a stackoverflow transition. When i wrote

 // Make base class typedefs available. typedef typename iterator::value_type value_type; typedef typename iterator::difference_type difference_type; 

my intention was to make the base typedef class available. But the iterator entry without any qualifications refers to the class itself, thanks to my other typedef above, duh!

 // This is myself... typedef Iterator<IS_CONST> iterator; 

So, by correctly referencing the iterator in std , the problem is solved, and I get to save typedef s.

 // Make base class typedefs available. typedef std::iterator<std::random_access_iterator_tag, color_type> base_class; typedef typename base_class::value_type value_type; typedef typename base_class::difference_type difference_type; 

Interestingly, the GCC does not seem to have a problem with this.

+1
source

Minimal example:

 template<bool B> struct Iterator { typedef typename Iterator<B>::some_type some_type ; int foo(); int foo(some_type n); }; int main() { return 0; } 

Exiting from http://rise4fun.com/Vcpp/2eQ :

  testvc.cpp (1): info: Could not find CompilerVersion hint, using default compiler version 'VS2012CTP'
 testvc.cpp (1): info: Ignoring directive '#include', which cannot be used in this online version of the Visual C ++ compiler
 testvc.cpp (1): info: Automatically importing the Standard Library headers which match this online version of the Visual C ++ compiler
 Microsoft (R) C / C ++ Optimizing Compiler Version 17.00.51025 for x86
 Copyright (C) Microsoft Corporation.  All rights reserved.

 testvc.cpp
 - \ testvc.cpp (10): fatal error C1063: compiler limit: compiler stack overflow
         - \ testvc.cpp (11): see reference to class template instantiation 'Iterator' being compiled
 Internal Compiler Error in.  You will be prompted to send an error report to Microsoft later.

To resolve this issue, remove the following lines from the code:

 // Make base class typedefs available. typedef typename iterator::value_type value_type; typedef typename iterator::difference_type difference_type; 

Then it compiles in Visual C ++ ( http://rise4fun.com/Vcpp/1pg ):

  testvc.cpp (1): info: Could not find CompilerVersion hint, using default compiler version 'VS2012CTP'
 testvc.cpp (1): info: Ignoring directive '#include', which cannot be used in this online version of the Visual C ++ compiler
 testvc.cpp (1): info: Automatically importing the Standard Library headers which match this online version of the Visual C ++ compiler
 testvc.cpp (2): info: Ignoring directive '#include', which cannot be used in this online version of the Visual C ++ compiler
 Microsoft (R) C / C ++ Optimizing Compiler Version 17.00.51025 for x86
 Copyright (C) Microsoft Corporation.  All rights reserved.

 testvc.cpp
 testvc.cpp (64): info: File compiled with no errors!

and works as expected in GCC ( http://ideone.com/mEX18H ):

  result: Success time: 0s memory: 2896 kB returned value: 0 
 input: no

 output:
 0 0
+3
source

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


All Articles