How to redefine the template class constructor via a macro in C++11?

Baron King

I want to recorded the line which created the shared_ptr in C++ 11. Here is my way to rewrite shared_ptr as Shared_ptr :

template<class T>
class Shared_Ptr{
public:
    Shared_Ptr(T* ptr = nullptr,int line=__LINE__)
        :_pPtr(ptr)
        , _pRefCount(new int(1))
        , _pMutex(new mutex)
    {
        cout<<this<<"is located in "<<line<<endl;
    }
    ~Shared_Ptr()
    {
        Release();
        cout<<this<<endl;
    }
    Shared_Ptr(const Shared_Ptr<T>& sp)
        :_pPtr(sp._pPtr)
        , _pRefCount(sp._pRefCount)
        , _pMutex(sp._pMutex)
    {
        AddRefCount();
    }
    Shared_Ptr<T>& operator=(const Shared_Ptr<T>& sp)
    {
        //if (this != &sp)
        if (_pPtr != sp._pPtr)
        {
            Release();
            _pPtr = sp._pPtr;
            _pRefCount = sp._pRefCount;
            _pMutex = sp._pMutex;
            AddRefCount();
        }
        return *this;
    }
    T& operator*(){
        return *_pPtr;
    }
    T* operator->(){
        return _pPtr;
    }
    int UseCount() { return *_pRefCount; }
    T* Get() { return _pPtr; }
    void AddRefCount()
    {
        _pMutex->lock();
        ++(*_pRefCount);
        _pMutex->unlock();
    }

    
private:
    void Release()
    {
        bool deleteflag = false;
        _pMutex->lock();
        if (--(*_pRefCount) == 0)
        {
            delete _pRefCount;
            delete _pPtr;
            deleteflag = true;
        }
        _pMutex->unlock();
        if (deleteflag == true)
            delete _pMutex;
    }
private:
    int *_pRefCount;
    T* _pPtr;
    mutex* _pMutex;
};
class student
{
int age;
public:
  student(int a):age(a)
  {

  }
}
;
int main()
{
   Shared_ptr<student> Tom(new student(24),__LINE__);
}

Is there a way to make Shared_ptr<student>Tom(new student(24)) as same as Shared_ptr <student> Tom(new student(24),__ LINE__) in C++11? In other words , invoke class Constructor with the arguments bound to args.

I tried to use marco to achieve,but I don't know the correct way how to define the macro of template class constructor.

Below is the macro definition I tried to write but wrong

template<typename T>
#define Shared_ptr<T>::Shared_ptr(T*) Shared_ptr<T>::Shared_ptr(T * ,__LINE__)
HolyBlackCat

Replace int line=__LINE__ in constructor parameters with int line = __builtin_LINE(). It's a non-standard compiler extension, but it works at least in GCC, Clang, and MSVC (i.e. most common compilers).

Then Shared_ptr<student> Tom(nullptr); will work.


Shared_ptr<student> Tom(42); will not work, because Shared_ptr doesn't have the right constructor, but it has nothing to do with getting the line number.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

C++11 std::vector of template class with argument in constructor

How to redefine a macro?

How to Redefine Class Objects in C++

Redefine Macro in existing C library

How write macro to avoid redefine?

C++ template copy constructor on template class

How redefine a loaded class?

Rails: How to redefine a Class

How to define a constructor template of a class template outside of the class template?

How to redefine a C++ #define macro using information from the macro itself?

How to specialize constructor of template class in another class?

Redefine template used to create a JUnit 5 class via "Create test" feature of IntelliJ 2019

C++11 template: How to ensure that the type inherits a class?

c++11 how to convert legacy class to template

How to pass template arguments via constructor?

c++ - copy constructor for template class

Calling base class template constructor in C++

Accessing a private constructor of a template class in C++

c++ initialize template class constructor

Constructor with another template class as a parameter in C++

How to define the constructor of an inner template class of another template class?

How to call constructor of a template base class in a template derived class?

How to pass template parameter from class constructor

C# redefine a variable in class

Template static class with string list as template parameter, how to make on c++ 11

C++ explicit instantiation of template constructor of non-template class

How to redefine constructor of standard function in JS?

Partial class template specialization c++11

C++11 Template Class with Multiple Definitions