So what happened to std::make_pair<> in C++11 ?

This is a discussion on So what happened to std::make_pair<> in C++11 ? within the C++ Programming forums, part of the General Programming Boards category; Hey there. I just transferred a small project I was working on on my other computer to try to integrate ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73

    So what happened to std::make_pair<> in C++11 ?

    Hey there. I just transferred a small project I was working on on my other computer to try to integrate a bit of C++11 in it for fun but the compiler complains about the make_pair<>() function:

    system.cpp:39:66: erreur: no matching function for call to ‘make_pair(std::string&, bool)’

    If I just remove the command line argument saying that I want to use the C++11 standard it stops complaining and the project builds and runs fine.

    What am I missing ?

    In the meantime I can just make my own make_pair() function since it is rather straightforward, but I would rather just use the existing one of course.

  2. #2
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Sorry, forgot the code.

    Code:
    void FSMachine::SetState(std::string state, std::string& value) {
        states[state] = std::make_pair<std::string, bool>(value, true);
    }
    Code:
    class FSMachine {
    private:
        std::map<std::string, std::pair<std::string, bool> > states;
    public:
        FSMachine();
    
        bool IsStateSet(std::string);
        void SetState(std::string, std::string& value);
        std::string ReadState(std::string);
    };

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    Did you #include <utility>?
    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

  4. #4
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Yes I did. I will try a clean + rebuild just in case.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Nope.

    Code:
    "/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .clean-conf
    make[1]: entrant dans le répertoire « /cygdrive/c/Users/win7/Documents/NetBeansProjects/CppApplication_1 »
    rm -f -r build/Debug
    rm -f dist/Debug/Cygwin_4.x-Windows/cppapplication_1.exe
    make[1]: quittant le répertoire « /cygdrive/c/Users/win7/Documents/NetBeansProjects/CppApplication_1 »
    
    CLEAN SUCCESSFUL (total time: 1s)
    "/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
    make[1]: entrant dans le répertoire « /cygdrive/c/Users/win7/Documents/NetBeansProjects/CppApplication_1 »
    "/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x-Windows/cppapplication_1.exe
    make[2]: entrant dans le répertoire « /cygdrive/c/Users/win7/Documents/NetBeansProjects/CppApplication_1 »
    mkdir -p build/Debug/Cygwin_4.x-Windows
    rm -f build/Debug/Cygwin_4.x-Windows/system.o.d
    g++-4.exe -std=c++0x -Wall -Wextra   -c -g -MMD -MP -MF build/Debug/Cygwin_4.x-Windows/system.o.d -o build/Debug/Cygwin_4.x-Windows/system.o system.cpp
    make[2]: quittant le répertoire « /cygdrive/c/Users/win7/Documents/NetBeansProjects/CppApplication_1 »
    make[1]: quittant le répertoire « /cygdrive/c/Users/win7/Documents/NetBeansProjects/CppApplication_1 »
    system.cpp: In member function ‘void FSMachine::SetState(std::string, std::string&)’:
    system.cpp:39:66: erreur: no matching function for call to ‘make_pair(std::string&, bool)’
    system.cpp: At global scope:
    system.cpp:62:10: attention : unused parameter ‘msg’
    make[2]: *** [build/Debug/Cygwin_4.x-Windows/system.o] Erreur 1
    make[1]: *** [.build-conf] Erreur 2
    make: *** [.build-impl] Erreur 2
    
    BUILD FAILED (exit value 2, total time: 2s)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    Sorry, but I cannot reproduce your problem using g++ 4.4.5 with --std=c++0x.
    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

  7. #7
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Could it be a GCC bug ? I would normally never suspect the compiler being wrong, but since only removing the --std=c++0x argument fixes the problem I can't see why my code would be wrong :\

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    What version of g++ are you using?
    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

  9. #9
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    How can I check that ? I tried to look for the version number but haven't found it. All I know is it's g++4 (but do not know what sub-version) and I have downloaded it yesterday from the Cygwin setup.exe.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    3,268
    When I compiled the code with g++ version 4.5.2 I got this error:
    main.cpp||In member function ‘void FSMachine::SetState(std::string, std::string&)’:|
    main.cpp|17|error: no matching function for call to ‘make_pair(std::string&, bool)’|
    ||=== Build finished: 1 errors, 0 warnings ===|
    I could get rid of the error by removing the --std=c++0x or by changing:
    Code:
    void FSMachine::SetState(std::string state, std::string& value) {
        states[state] = std::make_pair<std::string, bool>(value, true);
    }
    to
    Code:
    void FSMachine::SetState(std::string state, std::string& value) {
        states[state] = std::make_pair<std::string&, bool>(value, true);
    }
    How can I check that ?
    Try g++ --version at the command line.

    Jim
    Last edited by jimblumberg; 09-14-2011 at 10:59 PM.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Aw thank you ! I'm using version 4.5.3 and changing to a reference seems to have solved the compiler error. I don't really get why it should be a reference though :\

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    Quote Originally Posted by Alexandre
    changing to a reference seems to have solved the compiler error. I don't really get why it should be a reference though
    Out of curiosity, but have you tried:
    Code:
    states[state] = std::pair<std::string, bool>(value, true);
    Normally, I would expect make_pair to be used as:
    Code:
    states[state] = std::make_pair(value, true);
    That is, the main point of make_pair is to be less verbose in having to specify the types explicitly.
    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

  13. #13
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Yes I have tried std:air<> but it didn't work. I wish I could reproduce it for you but I am having a little issue with my IDE right now :/

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    3,268
    Normally, I would expect make_pair to be used as:
    Code:
    states[state] = std::make_pair(value, true);
    That does compile without errors for me and it looks like the better solution.

    Jim

  15. #15
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    That does look like what I will switch to as soon as I fix my IDE. Thanks everyone.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what about std::make_pair?
    By pheres in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2008, 08:24 AM
  2. Arhg, stupid syntax error, am I misusing make_pair?
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 01-26-2006, 09:19 AM
  3. What Happened
    By cfrost in forum Windows Programming
    Replies: 1
    Last Post: 08-20-2004, 10:09 PM
  4. What happened to the GD?
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-15-2002, 06:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21