Thread: Back insert iterator behavior: not sure if bug or my error

  1. #1
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139

    Back insert iterator behavior: not sure if bug or my error

    I'm getting some very odd behavior with the constructor of a class that uses a std::back_insert_iterator member variable. GCC seems to think the constructor definition is a construction call to said iterator:

    Code:
    ../src/palace/lib/message.cpp:6:60: error: no matching function for call to 'std::back_insert_iterator<std::vector<unsigned char> >::back_insert_iterator()'
        6 | PalaceMessage::PalaceMessage(std::vector<uint8_t> &incoming)
          |                                                            ^
    The class header itself is straightforward:
    Code:
    #ifndef _PALACE_MESSAGE_H
    #define _PALACE_MESSAGE_H
    
    #include <iterator>
    #include <string>
    #include <string_view>
    #include <vector>
    
    #include "protocol.hpp"
    
    class _DLLEXP PalaceMessage
    {
    public:
    	PalaceMessage(std::vector<uint8_t> &incoming);
    	template <class T> T Get();
    	std::string GetCStr();
    	std::string GetPStr();
    	std::vector<uint8_t> GetData(size_t length);
    	template <class T> PalaceMessage& Put(T t);
    	PalaceMessage& PutCStr(std::string_view s);
    	PalaceMessage& PutPStr(std::string_view s);
    	PalaceMessage& PutData(const std::vector<uint8_t> &data);
    private:
    	PALACE_MSG_HEADER m_header;
    	std::vector<uint8_t> m_data;
    	std::vector<uint8_t>::iterator m_read;
    	std::back_insert_iterator<std::vector<uint8_t>> m_write;
    };
    
    #endif // _PALACE_MESSAGE_H
    and the constructor in the .cpp file should be a no-brainer as well:
    Code:
    #include <algorithm>
    #include <utility>
    
    #include "message.hpp"
    
    PalaceMessage::PalaceMessage(std::vector<uint8_t> &incoming)
    {
    	m_data = std::move(incoming);
    	m_header = Get();
    }
    So am I overlooking something or is GCC being a goofball?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    Can you construct a complete (runnable) minimal example?
    Doing so in itself may reveal the problem.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    Now that I think about it, by the time the body of the ctor starts to run,
    all the default value, default ctors, and initialization list members have run.
    So it's complaining about back_insert_iterator's ctor inside PalaceMessage.
    It turns out that it doesn't have a default ctor until C++20.
    So you either need to use C++20 (-std=c++20)
    or you have to do something like this:
    Code:
    #include <vector>
    #include <iterator>
     
    struct A {
        std::vector<char> v;
        std::back_insert_iterator<std::vector<char>> w;
     
        A() : w(v) { }
    };
     
    int main() {
        A a;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139
    I see. Yeah, I found out by assigning it in constructor satisfies the compiler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is my class suitable for iterator-like behavior?
    By -Adrian in forum C++ Programming
    Replies: 7
    Last Post: 05-02-2015, 06:44 PM
  2. string insert breaks iterator
    By AndrewL in forum C++ Programming
    Replies: 15
    Last Post: 12-12-2008, 08:16 AM
  3. Normal iterator pointer behavior
    By SevenThunders in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2008, 12:11 PM
  4. std::iterator error!
    By Shamino in forum C++ Programming
    Replies: 13
    Last Post: 11-13-2007, 06:26 PM
  5. Back to work on that <insert feature>, oh wait, I already finished it!!
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-14-2004, 11:50 AM

Tags for this Thread