error::make_unique is not a member of ‘std’

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>();
139462 次浏览

make_unique即将推出的 C + + 14特性,因此在编译器上可能不可用,即使它是 C + + 11兼容的。

然而,您可以很容易地滚动您自己的实现:

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

(仅供参考,here is the final version of make_unique被投票为 C + + 14。这包括覆盖数组的附加函数,但总体思路仍然是相同的。)

如果有最新的编译器,可以在生成设置中更改以下内容:

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

这对我有用。

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

我在使用 XCode 时会遇到这种情况(我正在使用2019年最新的 XCode 版本... ...)。我正在使用 CMake 进行构建集成。使用 CMakeLists.txt 中的以下指令为我修复了这个问题:

set(CMAKE_CXX_STANDARD 14).

例如:

cmake_minimum_required(VERSION 3.14.0)
set(CMAKE_CXX_STANDARD 14)


# Rest of your declarations...

在我的例子中,我需要更新 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"
}
}
....
}

我换了台词

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

就是这样..。

如果你坚持使用 C + + 11,你可以从 下水获得 make_unique下水是一个从 Google 内部代码库中提取的 C + + 库的开源集合。