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

  • SEARCH
  • Home
  • 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 8317797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:43:40+00:00 2026-06-08T21:43:40+00:00

The following code generates the compiler error below (after the code), but not if

  • 0

The following code generates the compiler error below (after the code), but not if the vector contains unique_ptr’s directly (see commented code lines). Any ideas why?

The question is more concerned with the code block in the “#if 1” block, the “#else” block generates the error (after changing “#if 1” to “#if 0”) that is similar, but is more expected.

// MoveSemantics.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <memory>
#include <vector>

typedef std::unique_ptr<int> upi;

struct S
{
    S() : p(new int(123)) {}
    S(S&& s) : p( std::move(s.p) ) {} // NB: the move constructor is supposed to be used? (but not)
    upi p;
};

#if 1
void test()
{
    //std::vector<S> vs; // Okay
    //std::vector<upi> vupi(10); // Okay
    std::vector<S> vs(10); // Error! why in the hell does the compiler want to generate a copy constructor here??
}
#else
void test()
{
    std::vector<S> vs;

    vs.push_back( S() );
    const S& s = vs.front();
    //S& s = vs.front(); // fine!
    S s1 = std::move(s); // Error, but expected
}
#endif
int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

Compiler Error:

1> error C2248: 'std::unique_ptr<_Ty>::operator =' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          c:\program files\microsoft visual studio 11.0\vc\include\memory(1435) : see declaration of 'std::unique_ptr<_Ty>::operator ='
1>          with
1>          [
1>              _Ty=int
1>          ]
1>          This diagnostic occurred in the compiler generated function 'S &S::operator =(const S &)'
  • 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-06-08T21:43:42+00:00Added an answer on June 8, 2026 at 9:43 pm

    This looks like a bug in your std::lib. I’m sure it got here because of the history of the evolving vector specification.

    In C++98/03 vector had this constructor:

    explicit vector(size_type n, const T& value = T(), const Allocator& = Allocator());
    

    And the specification was that the T would be default constructed once and then copy constructed n times when called with the latter two arguments defaulted.

    In C++11 this got changed to:

    explicit vector(size_type n);
    vector(size_type n, const T& value, const Allocator& = Allocator());
    

    The spec for the 2nd constructor didn’t change. But the first did: It should default construct T n times, and not copy (or move) it at all.

    I would have expected the error message to say that the deleted or private copy constructor of unique_ptr was being used. That would have indicated that the vector was following the C++98/03 spec, and simply hasn’t been updated yet.

    But since the diagnostic is complaining about unique_ptr‘s copy assignment instead, then it looks like the vector has been updated, but incorrectly. It sounds like it is using this signature from C++98/03:

    explicit vector(size_type n, const T& value = T(), const Allocator& = Allocator());
    

    and default constructing n T‘s, and then assigning value to those n T‘s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following Scala code works fine in Scala 2.9, but it generates compiler error
The following code generates a compiler error about an unrecognized escape sequence for each
I have the following code that generates a compiler error: Boolean IConvertible.ToBoolean(IFormatProvider provider) {
The following line of code generates the compile time error (PE19) There is no
The following code generates a MS Word document in portrait format. But I need
The following code generates a compile error in Xcode: template <typename T> struct Foo
The following code generates a FileNotFoundException (using .NET 2.0): using System; using System.Collections.Generic; using
I have the following code (generates a quadratic function given the a, b, and
I don't understand why the following code generates a warning. interface Generic<T> { }
The following code snippet generates some warning messages when compiling: Cluster& Myclass::getCluster(const Point &p)

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.