c++11make-shared

no matching function for call to 'make_shared'


I am getting compiler error "no matching function for call to 'make_shared'" whenever I try to use a constructor that takes any arguments. So, for example:

std::shared_ptr<int> foo = std::make_shared<int> ();

works fine. But,

std::shared_ptr<int> foo = std::make_shared<int> (10);

Gives the following error:

/usr/bin/clang  -g -Wall -Wextra -Wc++11-extensions -c ./test.cpp
./test.cpp:7:30: error: no matching function for call to 'make_shared'
  std::shared_ptr<int> foo = std::make_shared<int> (10);
                         ^~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4700:1: note: candidate function [with _Tp = int, _A0 = int]
  not viable: expects an l-value for 1st argument
make_shared(_A0& __a0)

I took the above code directly from here http://www.cplusplus.com/reference/memory/make_shared/ and that code runs fine on cpp.sh web site. I suspect that there is something wrong with my compiler set up. Running in iTerm on a macbook. Also, I get the same error even if I remove the various options to clang shown above. Any ideas? Is it possible that my header file for needs to be updated? It's from Sep 4 2015. Seems recent enough for C++11 to work.

$ /usr/bin/clang --version
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin17.7.0
Thread model: posix

$ ls -l /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory
-rw-r--r--  1 root  wheel  174919 Sep  4  2015 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory

Solution

  • The error message is complaining you using an prvalue 10. Try to use

    int avar = 10;
    auto foo = std::make_shared<int> (avar);
    

    It is interesting to see what happens when using a lvalue.

    Did you build the std library locally? If you are, maybe you could try to rebuild again or grab a prebuilt library from somewhere.

    I tested the code on https://godbolt.org/ with configuration x86-64 clang 7.0.0 and -std=c++11. it works fine. Even you using iOS, it should be good on that os I guess.

    I also see you using -Wc++11-extensions when you building. Try use -std=c++11 instead maybe?


    Edit by DG: As noted in my comment below, this last suggestion, "Try use -std=c++11," worked! With -std=c++11 then all values (lvalues, rvalues, prvalues, etc.) work fine. See comments below.