jeudi 3 novembre 2016

Compiler complains that first argument of make_shared() expects an l-value

I have a simple class. One of its constructors takes two ints as arguments:

SimpleClass {
  private:
    int d_ii;
    int d_jj;
  public:
    // Constructors:
    SimpleClass() : d_ii=40, d_jj=10 {}
    SimpleClass( const int ii, const int jj ) : d_ii(ii), d_jj(jj) {}
}

Using the constructor with two const ints works fine here:

SimpleClass * pSc = new SimpleClass( 10, 20 );

And here, using the shared_ptr constructor, or using make_shared:

shared_ptr<SimpleClass> spSc1( new SimpleClass( 10, 20 ) );
shared_ptr<SimpleClass> spSc2 = make_shared( new SimpleClass( 10, 20 ) );

However passing the const ints directly to make_shared:

shared_ptr<SimpleClass> spSc2 = make_shared( 10, 20 );

causes the compiler to complain:

no matching function for call to 'make_shared' ...

candidate function [with _Tp = SimpleClass, _A0 = int, _A1 = int ] not viable: expects an l-value for 1st argument make_shared(_A0& __a0, _A1& __a1)

Indeed, when I pass l-values, it compiles fine:

int ii = 10;
int jj = 20;
shared_ptr<SimpleClass> spSc2 = make_shared( ii, jj );  // compiles fine!

Can anyone explain why this is? My constructor declares the arguments as const. And the examples given here http://ift.tt/2fgs6qD pass constants to make_shared. Any ideas? Thanks.

Aucun commentaire:

Enregistrer un commentaire