c++ - Clion 的 "Call to std::pair is ambiguous"但可以编译代码

标签 c++ windows clion

我有一个函数可以在这种状态下编译,但给出“配对调用不明确”,但仅在 Clion IDE 中,编译没有问题,如果我添加任何随机的东西,警告就会消失,即使它最终导致编译器错误。

std::pair<Status, std::set<std::string>> Config::foo(const std::string &sec, const std::string &key) const {

    return std::pair<Status, std::set<std::string>>(
            hasSection(sec) ? (hasKey(sec, key) ? Status::Success 
                                                : Status::MissingKey)
                                 : Status::MissingSec ,
         hasKey(sec, key) ? config_map.find(sec)->second.find(key)->second 
                                   : std::set<std::string>()  
          );
}

我不知道如何解决它,甚至不知道问题到底是什么。

最佳答案

如果你想构造一个 pair 对象,你必须使用 std::make_pair 函数模板。

template <class T1, class T2> pair<V1,V2> make_pair (T1&& x, T2&& y);

模板类型可以从传递给 make_pair 的参数中隐式推导出来。

 return std::make_pair(
        hasSection(sec) ? (hasKey(sec, key) ? Status::Success
                                            : Status::MissingKey)
                             : Status::MissingSec ,
     hasKey(sec, key) ? config_map.find(sec)->second.find(key)->second : 
     std::set<std::string>()
      );

此代码编译时没有“对配对的调用不明确”。

如果指定 make_pair 模板类型,会遇到“表达式必须是右值”类型的错误。

关于c++ - Clion 的 "Call to std::pair is ambiguous"但可以编译代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47065971/

相关文章:

c++ - 在 CLion 中设置 Google 测试

c - 使用 CMake 和 Clion 构建 C 项目

c++ - C++ 中的 cin.get() 有问题吗?

python - 使用 Python 检查某个进程是否以管理员身份运行

c++ - 如何在多线程环境中使用旧的单线程 C++ 库

clion - 无法在动态链接库 USERENV.dll 中找到过程入口点 CreateAppContainerProfile

c++ - 专用模板类的静态成员初始化

c++ - 对 'Inventory::insertEnd(Node*, int)' 的 undefined reference

c++ - reinterpret_cast 交换位?

c++ - 为什么 Win32 OleGetClipboard() 函数会返回 CLIPBRD_E_CANT_OPEN?