-1

我希望类 Config 能够在字符串键中存储任何值。为此目的,似乎 std::map 适合。不幸的是,这没有编译。

看起来该std::is_copy_constructible<std::tuple<const std::any&>>特征失败了。我该如何克服呢?请在https://github.com/marchevskys/VisualEngine找到源代码。

代码思路相同如下,

#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <any>
#include <map>
//#include <glm/glm.hpp>

//using vec3d = glm::vec<3, double, glm::highp>;

class Config {
public:
   static Config* get() {
    static Config config;
    return &config;
    }

   enum class Option : int {
      ImGuiEnabled = 0x0,   // bool
      ShipPosition = 0x1    // glm::vec3
   };

   using cb_function = std::function<void(std::any)>;

   bool is_option_available(Option option) const { return m_config.at(option).has_value(); }
   std::any get_option(Option option) const { return m_config.at(option); }
   void set_option_value(Option option, const std::any& value) { m_config.insert_or_assign(option, value); }
private:
   Config() {/* m_config[Option::ShipPosition] = vec3d({ 0., 0., 0. }); */} 
   ~Config() = default;
   std::map<Option, std::any> m_config;
};

int main()
{
    Config::get()->set_option_value(Config::Option::ImGuiEnabled, false);
    bool isImGuiEnabled = std::any_cast<bool>(Config::get()->get_option(Config::Option::ImGuiEnabled));
    std::cout << std::boolalpha << "ImGuiEnabled ? " << isImGuiEnabled << std::endl;
}

在 MS Visual Studio 上编译,并且使用早于 9.1.0 g++,但不会在 g++ 9.1.0 上编译。

你可以得到同样的错误:

#include <type_traits>
#include <tuple>
#include <any>

int main()
{
  bool b = std::is_copy_constructible< std::tuple<const std::any&> >::value );
  (void)b;
}
4

1 回答 1

1

当我们询问“可以std::tuple<const std::any&>复制”时,会触发奇怪的错误。

这最终会破坏,因为它被复制最终会检查“可以std::tuple<const std::any&>复制”作为中间步骤。

这是因为其中一个中间步骤涉及检查您是否可以std::any const&从 a 制作 a std::tuple<std::any const&>,这反过来又询问您是否可以复制 a std::tuple<std::any const&>,它询问您是否可以std::any const&从 a制作 astd::tuple<std::any const&>等。

所有这一切似乎都是由映射中的代码触发的,该代码将第二个参数捆绑在 a 中std::tuple,然后尝试用它来构建std::any。但它尝试使用std::any参数整个元组来构造。

这似乎是 GCC 标准库中的一个错误。

于 2019-06-24T20:02:29.717 回答