118

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>();
7
  • 1
    What compiler are you using? Commented Jul 7, 2014 at 11:15
  • 3
    What compiler/flags do you use? make_unique is a c++14 feature
    – MatthiasB
    Commented Jul 7, 2014 at 11:15
  • most likely you don't have the code for it as it is not part of c++11 ? Commented Jul 7, 2014 at 11:15
  • He is somehow using c++14 features, and you are not. Commented Jul 7, 2014 at 11:15
  • there would be an implementation of make_unique. it's not that difficult ;) msdn.microsoft.com/en-us/library/dn439780.aspx Commented Jul 7, 2014 at 11:16

6 Answers 6

188

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.)

12
  • 6
    @ali786 Depends on your compiler. With GCC, for example, you pass -std=c++1y on the command line. Commented Jul 7, 2014 at 11:26
  • 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. Commented Jul 7, 2014 at 11:38
  • 5
    @ali786 Actually, this is not a feature of the compiler itself, but rather of the standard library implementation (which is most likely 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). Commented Jul 7, 2014 at 11:40
  • 1
    @VictorEijkhout Yes, literally. You might want to put together a minimal example 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) Commented Aug 15, 2015 at 7:57
  • 1
    I have gcc 5.4 and I am still getting this error even when I try all flags mentioned here. Commented Apr 11, 2018 at 21:42
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.

1
  • 1
    This is specific to GCC/Clang and may not work for other compilers.
    – tambre
    Commented Aug 24, 2017 at 11:31
10

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

2

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.

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...
1

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.