How to overcome make_shared constness

I ran into some kind of problem and cannot decide which is the right solution.

Here is an example code to illustrate:

#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>

class TestClass{
    public:
        int a;
        TestClass(int& a,int b){};
    private:
        TestClass();
        TestClass(const TestClass& rhs);
};

int main(){
    int c=4;
    boost::shared_ptr<TestClass> ptr;

//NOTE:two step initialization of shared ptr    

//     ptr=boost::make_shared<TestClass>(c,c);// <--- Here is the problem
    ptr=boost::shared_ptr<TestClass>(new TestClass(c,c));

}

The problem is that I cannot create an instance of shared_ptr because make_shared receives and passes arguments to the TestClass constructor as "const A1 &, const A2 & ...", as described:

template<typename T, typename Arg1, typename Arg2 >
    shared_ptr<T> make_shared( Arg1 const & arg1, Arg2 const & arg2 );

I can cheat with boost :: shared (new ...) or rewrite the constructor for const links, but it doesn't seem to be the way it should be.

Thnx in advance!

+5
source share
1 answer

You can use boost::refto move the argument up, that is:

ptr = boost::make_shared< TestClass >( boost::ref( c ), c );
+11
source

All Articles