[Bug c++/63388] New: cout of enum class value - segmentation fault

kuq03132 at qoika dot com gcc-bugzilla@gcc.gnu.org
Sat Sep 27 09:09:00 GMT 2014


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63388

            Bug ID: 63388
           Summary: cout of enum class value - segmentation fault
           Product: gcc
           Version: 4.9.1
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kuq03132 at qoika dot com

See example:

//==go.cpp============================================

#include <iostream>

enum class Month {jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov,
dec};

std::ostream& operator<<(std::ostream& os, Month m)
{
  return os << m;                    // BUG - SEGMENTATION FAULT
}


int main()
{
  Month m = Month::may;
  std::cout << m << '\n';
  return 0;
}

//====================================================



Compiled and run with:

c++ -std=c++11 -o go go.cpp
./go 
Segmentation fault



DON'T WE WANT a COMPILER ERROR INSTEAD?
(Segfaulting with something so "simple" is very dangerous!)
(Problem also if -std=c++14)


To get the code to work correctly, we can convert the Month m to int:
int(m)


std::ostream& operator<<(std::ostream& os, Month m)
{
  return os << int(m);      // CONVERT TO INT - ok
}

Thanks.
A.

PS: Thanks for the gnu c++ compiler. I know I say "simple" above. But I'm not
fooled. A c++ compiler is a complex monster.



More information about the Gcc-bugs mailing list