home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,854 of 33,346   
   Andy Venikov to All   
   Surprising behaviour of c++11 vector con   
   06 Feb 13 16:48:36   
   
   From: avenikov@googlemail.com   
      
   I came across a class, that could be boiled-down to this:   
      
   class A   
   {   
      std::string str_;   
      
   public:   
      explicit A(char const * str) : str_(str) {}   
      explicit A(char ch) : str_(ch) {}   
   };   
      
   Trying to initialize a vector of this class, I unthinkingly   
   coded the following:   
      
   vector v1 { "abc", "def" };   
      
   It compiled.   
   But later on:   
      
   vector v2 { "abc" };   
      
   did not compile....   
      
   Wow I thought, something interesting is going on here.   
   Turns out that v1 { "abc", "def"} will actually call   
   vector::vector(It first, It last) constructor. This, in turn,   
   will produce fatal behaviour at run-time. If I make the A's   
   const char* constructor non-explicit, it works as expected.   
      
   I understand what's going on here, but it seems to me that this   
   pattern can be rather common and the results it produces are   
   really, really unexpected.   
   The range constructor can easily check that the iterator type is   
   compatible with its internal type. vector should not be able   
   to be constructed with "const char *" as an input iterator.   
      
   And this is not just the quality of the implementation issue   
   (this happened on gcc 4.7.2) as the standard prescribes this   
   constructor to look the way it looks.   
      
   Any thought on whether this should be fixed?   
      
   Thanks,   
       Andy.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca