C++ error::make_unique 不是“std”的成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24609271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:46:39  来源:igfitidea点击:

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

c++c++11compiler-errorsc++14unique-ptr

提问by Ali786

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

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

回答by ComicSansMS

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

make_unique即将推出的 C++14 功能,因此可能无法在您的编译器上使用,即使它符合 C++11。

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_uniquethat was voted into C++14. This includes additional functions to cover arrays, but the general idea is still the same.)

(仅供参考,这里是make_unique投票到 C++14的最终版本。这包括覆盖数组的附加功能,但总体思路仍然相同。)

回答by Anil8753

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.

这对我有用。

回答by Jagger Yu

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

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

回答by kovac

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:

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

set(CMAKE_CXX_STANDARD 14).

set(CMAKE_CXX_STANDARD 14).

Example:

例子:

cmake_minimum_required(VERSION 3.14.0)
set(CMAKE_CXX_STANDARD 14)

# Rest of your declarations...

回答by Aleksey Timoshchenko

In my caseI was needed update the std=c++

就我而言,我需要更新 std=c++

I mean in my gradle file was this

我的意思是在我的 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"
            }
        }
       ....
    }

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

就是这样...

回答by Alex Cohn

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

如果您坚持使用c++11,则可以make_uniqueabseil-cpp获取,这是一个从 Google 的内部代码库中提取的 C++ 库的开源集合。