Thread: Stream operator inconsistency between GCC and Clang

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

    Stream operator inconsistency between GCC and Clang

    Hey folks,

    just found out that stream operators are treated in a different way when using GCC or Clang as compilers. Until now, I always assumed that something like this was the way to go when moving objects to a stream:

    Code:
    std::vector<int> values{1, 2, 3};
    
    // stream the number of elements, then move the object
    stream << values.size() << std::move(values);
    Somehow however the value returned by `values.size()' isn't the same when using different compilers: with GCC the stream object will receive the value `0', whereas with Clang it gets (the correct) value `3'.

    I'm not really sure which one does it the right way, or if there is a right way at all. Below is some code I used for testing:

    Code:
    class test {
    public:
        test& operator<<(std::vector<int> values);
        test& operator<<(std::size_t count);
        
        void view();
        
    private:
        std::vector<int> m_values;
        std::size_t m_count{0};
    };
    
    
    test& test::operator<<(std::vector<int> values)
    {
        m_values=std::move(values);
        return *this;
    }
    
    
    test& test::operator<<(std::size_t count)
    {
        m_count=count;
        return *this;
    }
    
    
    void test::view()
    {
        std::cout << "test instance" << std::endl;
        
        std::cout << "  values: ";
        for (const auto& value:m_values) {
            std::cout << "<" << value << "> ";
        }
        
        std::cout << std::endl << "  count: " << m_count << std::endl;
    }
    
    
    int main()
    {
        { // first test
            std::vector<int> values{1, 2, 3};
            
            test object;
            
            object << std::move(values) << values.size();
            object.view();
        }
        
        { // second test
            std::vector<int> values{1, 2, 3};
            
            test object;
            
            object << values.size() << std::move(values);
            object.view();
        }
    }
    Code:
    [milli-961227@milli-notebook]$ g++ -std=c++1y stream.cc
    [milli-961227@milli-notebook]$ ./a.out
    test instance
      values: <1> <2> <3> 
      count: 3
    test instance
      values: <1> <2> <3> 
      count: 0
    [milli-961227@milli-notebook]$ clang++ -std=c++1y stream.cc
    [milli-961227@milli-notebook]$ ./a.out
    test instance
      values: <1> <2> <3> 
      count: 0
    test instance
      values: <1> <2> <3> 
      count: 3

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The order of evaluation of function arguments is unspecified, so you should not do this.
    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
    Okay, thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strlen inconsistency
    By deadrabbit in forum C Programming
    Replies: 5
    Last Post: 10-06-2011, 10:17 AM
  2. Issue overloading stream Operator/Manipulator
    By Jaken Veina in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2011, 12:48 PM
  3. program inconsistency
    By bchan90 in forum C Programming
    Replies: 4
    Last Post: 12-09-2008, 02:57 PM
  4. Inconsistency detected
    By fidodidohk in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2007, 05:12 AM
  5. FindWindow inconsistency
    By CondorMan in forum C++ Programming
    Replies: 7
    Last Post: 06-22-2006, 04:35 AM