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 5930155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:31:04+00:00 2026-05-22T14:31:04+00:00

I have some code in a header that looks like this: #include <memory> class

  • 0

I have some code in a header that looks like this:

#include <memory>

class Thing;

class MyClass
{
    std::unique_ptr< Thing > my_thing;
};

If I include this header in a cpp that does not include the Thing type definition, then this does not compile under VS2010-SP1:

1>C:\Program Files (x86)\Microsoft
Visual Studio
10.0\VC\include\memory(2067): error C2027: use of undefined type ‘Thing’

Replace std::unique_ptr by std::shared_ptr and it compiles.

So, I’m guessing that it’s the current VS2010 std::unique_ptr‘s implementation that requires the full definition and it’s totally implementation-dependant.

Or is it? Is there something in it’s standard requirements that makes impossible for std::unique_ptr‘s implementation to work with a forward declaration only? It feels strange as it should only hold a pointer to Thing, shouldn’t it?

  • 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-22T14:31:05+00:00Added an answer on May 22, 2026 at 2:31 pm

    Adopted from here.

    Most templates in the C++ standard library require that they be instantiated with complete types. However shared_ptr and unique_ptr are partial exceptions. Some, but not all of their members can be instantiated with incomplete types. The motivation for this is to support idioms such as pimpl using smart pointers, and without risking undefined behavior.

    Undefined behavior can occur when you have an incomplete type and you call delete on it:

    class A;
    A* a = ...;
    delete a;
    

    The above is legal code. It will compile. Your compiler may or may not emit a warning for above code like the above. When it executes, bad things will probably happen. If you’re very lucky your program will crash. However a more probable outcome is that your program will silently leak memory as ~A() won’t be called.

    Using auto_ptr<A> in the above example doesn’t help. You still get the same undefined behavior as if you had used a raw pointer.

    Nevertheless, using incomplete classes in certain places is very useful! This is where shared_ptr and unique_ptr help. Use of one of these smart pointers will let you get away with an incomplete type, except where it is necessary to have a complete type. And most importantly, when it is necessary to have a complete type, you get a compile-time error if you try to use the smart pointer with an incomplete type at that point.

    No more undefined behavior

    If your code compiles, then you’ve used a complete type everywhere you need to.

    class A
    {
        class impl;
        std::unique_ptr<impl> ptr_;  // ok!
    
    public:
        A();
        ~A();
        // ...
    };
    

    Type completeness requirements for unique_ptr and shared_ptr

    shared_ptr and unique_ptr require a complete type in different places. The reasons are obscure, having to do with a dynamic deleter vs a static deleter. The precise reasons aren’t important. In fact, in most code it isn’t really important for you to know exactly where a complete type is required. Just code, and if you get it wrong, the compiler will tell you.

    However, in case it is helpful to you, here is a table which documents several operations of shared_ptr and unique_ptr with respect to completeness requirements.

    Operation unique_ptr shared_ptr
    P() (default constructor) incomplete incomplete
    P(const P&) (copy constructor) — incomplete
    P(P&&) (move constructor) incomplete incomplete
    ~P() (destructor) complete incomplete
    P(A*) (constructor from ptr) incomplete complete
    operator=(const P&) (copy assignment) — incomplete
    operator=(P&&) (move assignment) complete incomplete
    reset() complete incomplete
    reset(A*) complete complete

    Any operations requiring pointer conversions require complete types for both unique_ptr and shared_ptr.

    The unique_ptr<A>{A*} constructor can get away with an incomplete A only if the compiler is not required to set up a call to ~unique_ptr<A>(). For example if you put the unique_ptr on the heap, you can get away with an incomplete A. More details on this point can be found in BarryTheHatchet’s answer here.

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

Sidebar

Related Questions

I have some code like this in a winforms app I was writing to
I have some code like this: If key.Equals(search, StringComparison.OrdinalIgnoreCase) Then DoSomething() End If I
I have some VERY simple code to return the title for a section header:
I have some code which collects points (consed integers) from a loop which looks
I have some code in a javascript file that needs to send queries back
I have some code that gives a user id to a utility that then
We have a function header format that we have to follow. It basically looks
We have a function header format that we have to follow. It basically looks
I have a class that represents some file data. The file contains some headers.
I am trying to rewrite some legacy C code and would like to have

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.