【问题标题】:error::make_unique is not a member of ‘std’错误::make_unique 不是‘std’的成员
【发布时间】:2014-08-27 20:34:50
【问题描述】:

我正在尝试编译发布在代码审查上的以下线程池程序以对其进行测试。

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 的一部分?
  • 他在某种程度上使用了 c++14 的特性,而你没有。
  • 会有一个 make_unique 的实现。没那么难 ;) msdn.microsoft.com/en-us/library/dn439780.aspx

标签: c++ c++11 compiler-errors c++14 unique-ptr


【解决方案1】:

make_unique 是一个upcoming C++14 feature,因此可能在您的编译器上不可用,即使它符合 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。这包括覆盖数组的附加函数,但总体思路仍然相同。)

【讨论】:

  • @ali786 取决于您的编译器。例如,使用 GCC,您可以在命令行上传递 -std=c++1y
  • @ali786 也许 GCC 4.8.1 不支持这部分 C++14?你查阅过它的文档吗?顺便说一句,最新的 GCC 是4.9.0
  • @ali786 实际上,这不是编译器本身的功能,而是标准库实现的功能(在您的情况下很可能是libstdc++)。 Afaik,仅在 gcc 4.9.0 中添加了对这个特定功能的支持(this post 也建议)。
  • @VictorEijkhout 是的,literally。如果您在运行时遇到问题,您可能希望将minimal example 放在一起并发布一个新问题。 (仅供参考,您可以将文本括在反引号中以使其在 cmets 中显示为代码)
  • 我有 gcc 5.4,即使尝试此处提到的所有标志,我仍然会收到此错误。
【解决方案2】:

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

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

这对我有用。

【讨论】:

  • 这是 GCC/Clang 特有的,可能不适用于其他编译器。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    这发生在我使用 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...
    

    【讨论】:

      【解决方案5】:

      就我而言我需要更新 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"
                  }
              }
             ....
          }
      

      就是这样……

      【讨论】:

        【解决方案6】:

        如果您被 c++11 卡住,您可以从 abseil-cpp 获取 make_unique,这是一个从 Google 内部代码库中提取的 C++ 库的开源集合。

        【讨论】:

          猜你喜欢
          • 2014-11-23
          • 2020-02-02
          • 2021-09-07
          • 2013-08-07
          • 2017-05-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多