两次相同的 for 循环:一个编译,另一个不编译
Twice the same for loop: one compiles, the other does not
我有这段代码可以使几个字符串小写(请参阅此SO帖子(。
void some_free_standing_function(std::string solver, std::map<std::string, option_t> opts) {
for (auto & c : solver) c = tolower(c);
for (auto p : opts)
for (auto & c : p.first)
c = tolower(c);
}
第一个基于范围的for
似乎可以编译,最后一个没有:Clang给了我error: cannot assign to variable 'c' with const-qualified type 'const char &'
.
为什么第一个通过而不是第二个,因为它们完全相同?
注意std::map
的value_type
是std::pair<const Key, T>
,这意味着对于p.first
你会得到一个const
std::string
,然后c
的类型将是const char&
,这是不能修改的。
第一个代码片段没有这样的问题; solver
是一个非常量std::string
。
相关文章:
- 在macOS上使用libcurl库编译一个c++at项目
- 编译一个自定义的tf操作,其中输入是5d张量
- 为什么cc可以编译一个c++程序?
- 无法使用crfsuite的C++ API编译一个非常简单的Tagger示例
- 如何交叉编译一个在 Linux for Linux 和 Linux for Windows 中编译的程序?
- Makefile:编译一个llvm程序,作为g++编译的bnfc项目的一部分
- 将char8_t const* 输出到 cout 和 wcout,一个编译一个不编译
- 在 Linux 上编译一个 c++ 应用程序,并在 TI TIVA TM4C123GH6PM上刷新它
- 为什么我可以编译一个有 2 个返回的代码?
- 在 caffe 中仅编译一个图层文件
- 用"gcc -c"通关很好地编译一个C++程序。为什么?
- 如何在VS2017中编译一个没有/Za的文件(其余的有/Za)
- QML应用程序编译一个 *form.ui.qml文件,但忽略了关联的.qml文件
- 编译一个支持VBA中声明函数的dll
- Microsoft Visual C++-编译一个没有依赖项的DLL
- 如何在Ubuntu上的G 中编译一个程序,具有多个源文件
- 如果没有项目,如何在Visual Studio 2012中仅编译一个文件(C )
- 仅在添加第二个项目后编译一个项目的错误
- 为什么 g++ 不能编译一个简单的 GTK 程序?
- 在MacOS上编译一个动态库,库搜索路径指向同一目录(CMake)