Thread: Strange program behavior

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    66

    Strange program behavior

    Hey folks,

    please have a look at this simple class:
    Code:
    #include <initializer_list>
    #include <string>
    
    class test {
    public:
        test(std::string value, bool flag=false)
        {
        }
        
        test(std::initializer_list<std::string> values, bool flag=false)
        {
        }
    };
    Now, when testing this in I get quite strange results:

    Code:
    int main() // compiles fine
    {
        test first{"abc"};
    }
    Code:
    int main() // throws a `std::lenght_error' with `what():  basic_string::_S_create'
    {    test second{{"abc", "def"}};
    }
    Code:
    int main() // fine again
    {
        test first{"abc"};
        test second{{"abc", "def"}};
    }
    I really don't understand how the construction of the instance `first' can resolve whatever problem the `second' instance encounters. Is there any logical explanation for this?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    My guess is that you are running into undefined behaviour, but unfortunately I am not familiar enough with the details to tell you what, if my guess is correct to begin with.

    That said, could you not simply write:
    Code:
    test second{"abc", "def"};
    ?

    EDIT:
    Experimenting a little, I think your problem is that the compiler could be interpreting the extra pair of braces as initialising a std::string object. When you use them along with the argument for the flag parameter, it perhaps becomes clear that they are not meant to initialise a std::string object. Omitting the extra braces when you do not need to disambiguate for the flag parameter seems to be the correct approach. Alternatively, you can keep it simple and just use initializer_list as a lone parameter.
    Last edited by laserlight; 04-04-2015 at 01:05 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    66
    Ah clever trick, this works.. Thanks! I assumed that the `initializer_list' would require its own braces as soon as there are other parameters to choose from. I'll consider my problem to be solved, but I'd still be happy if someone could explain me how `first' can influence `second'.

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    Just a stab in the dark: maybe it has to do with how the compiler strongly prefers the initializer list overload? Maybe test first() also calls the initializer list constructor? Then because the latter scenario contains calls to both constructors, the compiler can make sense out of the double braces.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jiggunjer View Post
    Maybe test first() also calls the initializer list constructor?
    It does, because that's how overload resolution works. If there's an initializer list constructor, then it is considered the best match.

    Anyway, I'm not sure why this happens, but I do know you can fix it by using paranthesises for the outer level:
    Code:
    test second({"abc", "def"});
    // Or as laserlight showed... test second{"abc", "def"};
    Both are equivalent.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2014
    Posts
    66
    Thanks to you all for your efforts!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help, please - very strange behavior
    By snork in forum C Programming
    Replies: 16
    Last Post: 09-26-2011, 01:36 AM
  2. Strange behavior
    By onako in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2010, 07:00 AM
  3. Very strange behavior from very simple program
    By somekid413 in forum C Programming
    Replies: 5
    Last Post: 12-17-2009, 08:53 PM
  4. strange std::map behavior
    By manav in forum C++ Programming
    Replies: 63
    Last Post: 04-11-2008, 08:00 AM
  5. strange behavior
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 12:03 PM