103

I am trying to compile the following thread pool program posted on code review to test it.

https://codereview.stackexchange.com/questions/55100/platform-independant-thread-pool-v4

But I am getting the errors

threadpool.hpp: In member function ‘std::future<decltype (task((forward<Args>)(args)...))> threadpool::enqueue_task(Func&&, Args&& ...)’:
threadpool.hpp:94:28: error: ‘make_unique’ was not declared in this scope
     auto package_ptr = make_unique<task_package_impl<R, decltype(bound_task)>>  (std::move(bound_task), std::move(promise));
                        ^
threadpool.hpp:94:81: error: expected primary-expression before ‘>’ token
     auto package_ptr = make_unique<task_package_impl<R, decltype(bound_task)>>(std::move(bound_task), std::move(promise));
                                                                             ^
main.cpp: In function ‘int main()’:
main.cpp:9:17: error: ‘make_unique’ is not a member of ‘std’
 auto ptr1 = std::make_unique<unsigned>();
             ^
main.cpp:9:34: error: expected primary-expression before ‘unsigned’
 auto ptr1 = std::make_unique<unsigned>();
                              ^
main.cpp:14:17: error: ‘make_unique’ is not a member of ‘std’
 auto ptr2 = std::make_unique<unsigned>();
             ^
main.cpp:14:34: error: expected primary-expression before ‘unsigned’
 auto ptr2 = std::make_unique<unsigned>();
underscore_d
  • 5,331
  • 3
  • 29
  • 56
Ali786
  • 4,211
  • 6
  • 34
  • 54

6 Answers6

154

make_unique is an upcoming C++14 feature and thus might not be available on your compiler, even if it is C++11 compliant.

You can however easily roll your own implementation:

template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
    return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

(FYI, here is the final version of make_unique that was voted into C++14. This includes additional functions to cover arrays, but the general idea is still the same.)

ComicSansMS
  • 43,180
  • 11
  • 123
  • 140
  • So how can I use the C++14 features I recently updated my compiler to latest one. I will implement for this but in future. – Ali786 Jul 07 '14 at 11:23
  • 6
    @ali786 Depends on your compiler. With GCC, for example, you pass `-std=c++1y` on the command line. – Angew is no longer proud of SO Jul 07 '14 at 11:26
  • @Angew I am using gcc version 4.8.1 and the above command is still not working. – Ali786 Jul 07 '14 at 11:34
  • 1
    @ali786 Perhaps GCC 4.8.1 doesn't support this part of C++14? Have you consulted its docs? BTW, the latest GCC is [4.9.0](https://gcc.gnu.org/gcc-4.9/). – Angew is no longer proud of SO Jul 07 '14 at 11:38
  • 4
    @ali786 Actually, this is not a feature of the compiler itself, but rather of the standard library implementation (which is most likely [libstdc++](https://gcc.gnu.org/libstdc++/) in your case). Afaik, support for this particular feature was only added with gcc 4.9.0 (as is also suggested by [this post](https://isocpp.org/blog/2014/04/gcc-4.9.0)). – ComicSansMS Jul 07 '14 at 11:40
  • Add that fragment literally? ../imp/imp_base.h:45:6: error: 'unique_ptr' in namespace 'std' does not name a template type std::unique_ptr make_unique(Args&&... args) { I'm using gcc 4.9.3 (sorry, I can't figure out how to display code. four spaces does not work.) – Victor Eijkhout Aug 14 '15 at 21:33
  • 1
    @VictorEijkhout Yes, [literally](http://coliru.stacked-crooked.com/a/9296c28c089975ce). You might want to put together a [minimal example](http://stackoverflow.com/help/mcve) and post a new question if you have trouble getting it to run. (And fyi, you can enclose text in backticks ` to make it display as code in comments) – ComicSansMS Aug 15 '15 at 07:57
  • You have to use: `-std=c++1y` for g++ prior to 4.9 or `-std=c++14` for more recent versions. Something like that: `g++ main.cpp -o hello.exe -std=c++14` – Patapoom Mar 04 '18 at 16:53
  • 1
    I have gcc 5.4 and I am still getting this error even when I try all flags mentioned here. – Timothy Swan Apr 11 '18 at 21:42
  • @TimothySwan Did you remember to `#include `? This [should work on an x86 gcc 5.4](https://godbolt.org/g/hL6J3y). – ComicSansMS Apr 12 '18 at 07:18
  • Why is this template function not nothrow? Because I don't see a difference between the template and when I would use std::unique_ptr(new type(...)) directly. How can this template be safer and not trowing an exception?? – DrumM Sep 14 '18 at 07:19
16

If you have latest compiler, you can change the following in your build settings:

 C++ Language Dialect    C++14[-std=c++14]

This works for me.

Anil8753
  • 2,059
  • 2
  • 18
  • 30
8

1.gcc version >= 5
2.CXXFLAGS += -std=c++14
3. #include <memory>

Jagger Yu
  • 361
  • 3
  • 2
1

This happens to me while working with XCode (I'm using the most current version of XCode in 2019...). I'm using, CMake for build integration. Using the following directive in CMakeLists.txt fixed it for me:

set(CMAKE_CXX_STANDARD 14).

Example:

cmake_minimum_required(VERSION 3.14.0)
set(CMAKE_CXX_STANDARD 14)

# Rest of your declarations...
kovac
  • 3,720
  • 4
  • 32
  • 62
1

If you are stuck with c++11, you can get make_unique from abseil-cpp, an open source collection of C++ libraries drawn from Google’s internal codebase.

Alex Cohn
  • 52,705
  • 8
  • 94
  • 269
0

In my case I was needed update the std=c++

I mean in my gradle file was this

android {
    ...

    defaultConfig {
        ...

        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11", "-Wall"
                arguments "-DANDROID_STL=c++_static",
                        "-DARCORE_LIBPATH=${arcore_libpath}/jni",
                        "-DARCORE_INCLUDE=${project.rootDir}/app/src/main/libs"
            }
        }
       ....
    }

I changed this line

android {
    ...

    defaultConfig {
        ...

        externalNativeBuild {
            cmake {
                cppFlags "-std=c++17", "-Wall"   <-- this number from 11 to 17 (or 14)
                arguments "-DANDROID_STL=c++_static",
                        "-DARCORE_LIBPATH=${arcore_libpath}/jni",
                        "-DARCORE_INCLUDE=${project.rootDir}/app/src/main/libs"
            }
        }
       ....
    }

That's it...

Aleksey Timoshchenko
  • 3,364
  • 1
  • 33
  • 61