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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:19:32+00:00 2026-05-27T18:19:32+00:00

namespace boost { template<typename T> class shared_ptr { // I have problems to understand

  • 0
namespace boost {

  template<typename T> class shared_ptr { // I have problems to understand the T
  public:
    template <class Y> explicit shared_ptr(Y* p);
    template <class Y,class D> shared_ptr(Y* p,D d);

    ~shared_ptr();

    shared_ptr(const shared_ptr & r);
    template <class Y> explicit 
      shared_ptr(const weak_ptr<Y>& r);
    template <class Y> explicit shared_ptr(std::auto_ptr<Y>& r);

    shared_ptr& operator=(const shared_ptr& r);

    void reset(); 

    T& operator*() const;
    T* operator->() const;
    T* get() const;

    bool unique() const;
    long use_count() const;

    operator unspecified-bool-type() const;

    void swap(shared_ptr<T>& b);
  };

  template <class T,class U>
    shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r);
}

Example of the usage of boost::shared_ptr:    
   boost::shared_ptr<std::string> ptr(new std::string("hello world")); // Line 1

Question> When we define the Line 1 which type is used for replacing type typename T? My understanding is that the type class Y is replaced by std::string when we initialize a boost::shared_ptr. Now, the question is that why we don’t have to provide a type T? If we do it implicitly, where?

Thank you

  • 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-27T18:19:33+00:00Added an answer on May 27, 2026 at 6:19 pm
    boost::shared_ptr<std::string> ptr(new std::string("hello world"));
    //                TTTTTTTTTTT      YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
    
    
    boost::shared_ptr<void> ptr(new std::string("hello world"));
    //                TTTT      YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
    

    Y* is the thing you passed to the constructor, and Y is deduced from that argument. T is part of the smart pointer type and provides type checking when you pass the shared_ptr around. void will completely erase any type information at compile time. While if you use std::string you completely preserve any type information (unless you would pass the constructor a class derived from std::string, in which case of course some information is again erased).

    Shared ptr remembers the Y* passed to its constructor and using virtual calls or equivalent mechanisms it is able to call correct destructors when all Shared ptr copies die.

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

Sidebar

Related Questions

using namespace std; class A { public: A() {} ~A() {} map<int, string*>& getMap()
I have both ::tensor::contract and ::tensor::detail::contract #include tensor/detail/contract.hpp namespace tensor { template<typename Alpha, class
I have a boost multi index container thus. using namespace boost::multi_index; template < typename
Please consider the following simplified example: #include boost/shared_ptr.hpp #include boost/smart_ptr/enable_shared_from_this.hpp using namespace boost; class
#include <boost/ptr_container/ptr_vector.hpp> #include <iostream> using namespace std; using namespace boost; struct A { ~A()
I have a defined a simple template class. In this class I define a
// ... snipped includes for iostream and fusion ... namespace fusion = boost::fusion; class
I have a template class that I serialize (call it C), for which I
I'm trying to expose the following c++ function to python using boost.python: template <typename
In file A.hpp, I have extern boost::signal<void (model::Bullet&, Point&, Point&, int)> signal_createBullet; and so

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.