Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6181277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:59:28+00:00 2026-05-24T00:59:28+00:00

I am trying to store a noncopyable (but movable) object inside an std::pair, as

  • 0

I am trying to store a noncopyable (but movable) object inside an std::pair, as follows:

#include <utility>

struct S
{
    S();
private:
    S(const S&);
    S& operator=(const S&);
};

int main()
{
    std::pair<int, S> p{0, S()};
    return 0;
}

But I’m getting the following compiler error with gcc 4.6:

In file included from include/c++/4.6.0/bits/move.h:53:0,
                 from include/c++/4.6.0/bits/stl_pair.h:60,
                 include/c++/4.6.0/utility:71,
                 from src/test.cpp:1:
include/c++/4.6.0/type_traits: In instantiation of 'const bool std::__is_convertible_helper<S, S, false>::__value':
include/c++/4.6.0/type_traits:789:12:   instantiated from 'std::is_convertible<S, S>'
src/test.cpp:13:31:   instantiated from here
src/test.cpp:7:5: error: 'S::S(const S&)' is private
include/c++/4.6.0/type_traits:782:68: error: within this context
In file included from include/c++/4.6.0/utility:71:0,
                 from src/test.cpp:1:
src/test.cpp: In constructor 'std::pair<_T1, _T2>::pair(_U1&&, const _T2&) [with _U1 = int, <template-parameter-2-2> = void, _T1 = int, _T2 = S]':
src/test.cpp:13:31:   instantiated from here
src/test.cpp:7:5: error: 'S::S(const S&)' is private
include/c++/4.6.0/bits/stl_pair.h:121:45: error: within this context

It seems the compiler is trying to call the std::pair<_T1, _T2>::pair(_U1&&, const _T2&) constructor, which of course is problematic. Shouldn’t the compiler be calling the std::pair<_T1, _T2>::pair(_U1&&, _U2&&) constructor instead? What is going on here?

EDIT: Ok, I understand that providing an explicit move constructor fixes the problem, but I’m still a little confused.

Suppose that I make the class noncopyable by inheriting from boost::noncopyable rather than declaring my own private copy constructor.

The following works fine, suggesting that a move constructor is implicitly generated:

#include <boost/noncopyable.hpp>

struct S : boost::noncopyable
{
};

void f(S&&)
{

}

int main()
{
    f(S());
    return 0;
}

However, with std::pair it still doesn’t work:

#include <utility>
#include <boost/noncopyable.hpp>

struct S : boost::noncopyable
{
};

int main()
{
    std::pair<int, S> p{0, S()};
    return 0;
}

Errors:

In file included from include/c++/4.6.0/utility:71:0,
                 from src/test.cpp:1:
/include/c++/4.6.0/bits/stl_pair.h: In constructor 'std::pair<_T1, _T2>::pair(_U1&&, const _T2&) [with _U1 = int, <template-parameter-2-2> = void, _T1 = int, _T2 = S]':
src/test.cpp:16:31:   instantiated from here
/include/c++/4.6.0/bits/stl_pair.h:121:45: error: use of deleted function 'S::S(const S&)'
src/test.cpp:4:8: error: 'S::S(const S&)' is implicitly deleted because the default definition would be ill-formed:
boost/boost/noncopyable.hpp:27:7: error: 'boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable&)' is private
src/test.cpp:4:8: error: within this context

Moreover, adding = default-ed default constructor and move constructor does not help!

#include <utility>
#include <boost/noncopyable.hpp>

struct S : boost::noncopyable
{
    S() = default;
    S(S&&) = default;
};

int main()
{
    std::pair<int, S> p{0, S()};
    return 0;
}

I get the same errors! I have to explicitly give the definition of the move constructor myself, which is annoying if the class has a lot of members:

#include <utility>
#include <boost/noncopyable.hpp>

struct S : boost::noncopyable
{
    S() = default;
    S(S&&) {}
};

int main()
{
    std::pair<int, S> p{0, S()};
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:59:30+00:00Added an answer on May 24, 2026 at 12:59 am

    You need to provide a move constructor. The following compiles without errors.

    #include <utility>
    
    struct S
    {
        S() {}
        S(S&&) {}
        S& operator=(S&&) {}
    
        S(const S&) =delete;
        S& operator=(const S&) =delete;
    };
    
    int main()
    {
        std::pair<int, S> p{0, S()};
        return 0;
    }
    

    EDIT:

    It seems that if you inherit from another class (or struct) then the base needs to declare a move constructor. I think this is because if you default the derived class’ move constructor it tries to move the base object and fails to do so.

    Here’s an edited boost::noncopyable that defines a move constructor.

    #include <utility>
    
    namespace boost {
    
    namespace noncopyable_  // protection from unintended ADL
    {
      class noncopyable
      {
       protected:
          noncopyable() {}
          noncopyable(noncopyable&&) {};
          ~noncopyable() {}
       private:  // emphasize the following members are private
          noncopyable( const noncopyable& );
          const noncopyable& operator=( const noncopyable& );
      };
    }
    
    typedef noncopyable_::noncopyable noncopyable;
    
    } // namespace boost
    
    struct S : boost::noncopyable
    {
        S() = default;
        S(S&&) = default;
    
        S& operator=(S&&) {}
    };
    
    int main()
    {
        std::pair<int, S> p{0, S()};
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to store an xml serialized object in a cookie, but i get
Hello iam trying to store sum information inside a SQL Server table , but
I'm trying to store a set inside the object property (and read it) of
I'm trying to store the names of some variables inside strings. For example: Dim
I'm trying to store some data in DOM Elements ($.cache) on my plugin but
I'm trying to store numbers in a NSMutableArray, but when I check it, all
I'm trying to store pixel data by using glReadPixels, but so far I managed
So I am trying to store an array of objects into localStorage, as follows:-
i am trying to store some method callbacks but referring to it will keep
I'm trying to store all the users inside a sharedObject thats created on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.