错误::make_unique不是std的成员

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

本文关键字:std 成员 不是 unique make 错误      更新时间:2023-10-16

我正在尝试编译下面的线程池程序,张贴在代码审查中以测试它。

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

但是我得到了错误

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>();

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

(仅供参考,这是投票进入c++ 14的make_unique的最终版本。这包含了覆盖数组的其他函数,但总体思路仍然相同。)

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

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

1。GCC版本>= 5
2.CXXFLAGS += -std=c++14
3.# include & lt; 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++

我的意思是在gradle文件中有这个

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,您可以从abseel -cpp获得make_unique, abseel -cpp是一个从Google内部代码库绘制的c++库的开源集合。