小编Eth*_*abc的帖子

基于范围的 for 循环 (C++11 起) 中的成员解释是什么?

我阅读了有关基于范围的循环的文档for

如果范围类型具有名为 begin 的成员和名为 end 的成员,则使用成员解释。无论成员是类型、数据成员、函数还是枚举器,也无论其可访问性如何,都会执行此操作。class meow { enum { begin = 1, end = 2}; /* rest of class */ };因此,即使存在命名空间范围的开始/结束函数,类似的类也不能与基于范围的 for 循环一起使用。

我不明白这一段。成员解释做了什么才能禁止示例类与基于范围的 for 循环一起使用?

c++ c++11 range-based-loop

6
推荐指数
1
解决办法
101
查看次数

如何在仅标头模式下使用 fmt 库?

很难使用 fmt 库的仅标头模式。这是我详细尝试的内容:我从https://fmt.dev/latest/index.html下载了 fmt7.1.3,只将目录fmt-7.1.3/include/fmt放在目录 ( [trgdir]) 中并编写了一个 test.cpp 如下:

#include <iostream>
#include <fmt/format.h>
int main() {
    fmt::format("The answer is {}.", 42);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然后在我使用的终端中

gcc -I[trgdir] test.cpp
Run Code Online (Sandbox Code Playgroud)

其中 gcc 我定义为

alias gcc='gcc-10 -xc++ -lstdc++ -shared-libgcc -std=c++17 -O2 '
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

Undefined symbols for architecture x86_64:
  "__ZN3fmt2v76detail7vformatB5cxx11ENS0_17basic_string_viewIcEENS0_11format_argsE", referenced from:
      _main in ccEeTo0w.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

我已经检查了这篇文章,但我仍然无法解决我的问题。如何在不获取“架构 x86_64 的未定义符号”的情况下使用 fmt 库

c++ fmt

4
推荐指数
1
解决办法
326
查看次数

函数重载。函数内部的操作是一样的。我必须重写所有内容吗?

我有一堆带有 inputxy. 我允许输入类型通过引用传递(如果它已经存在)和通过临时输入(使用&&)(如果不存在并且需要创建一个临时对象)。

基本上,我定义和重载函数如下

double foo(const std:::vector<int> & x, const std:::vector<int> &  y){
// Do something
}
double foo(const std:::vector<int> & x,std:::vector<int>&& y){
// Do something
}
double foo(std:::vector<int>&& x,const std:::vector<int> &  y){
// Do something
}
double foo(std:::vector<int>&& x,std:::vector<int>&& y){
// Do something
}
Run Code Online (Sandbox Code Playgroud)

//Do something 完全相同,但唯一的区别是输入类型。有没有一种方法可以在没有太多冗余的情况下简化代码?我不想有foo(std:::vector<int> x,std:::vector<int> y)内存问题。

编辑:有时我还必须稍微修改输入。可能有三种类型的不同子集std::vector<int> &,std::vector<int> &&,const std::vector<int> &xy

正如@AVH(谢谢!)指出的那样,如果它只是 from std::vector<int> &&,const std::vector<int> &const std::vector<int> &应该足以完成这项工作。

结合@Ken Wayne …

c++ move-semantics c++11

1
推荐指数
1
解决办法
61
查看次数

标签 统计

c++ ×3

c++11 ×2

fmt ×1

move-semantics ×1

range-based-loop ×1