我一直在思考,虽然我了解std :: observer_ptr的目标,但我认为如果至少有一个类似指针类型的选项(它知道指向的内容是否已被删除)会很好。例如,我们可能会有类似以下内容
slightly_smart_ptr<Foo> p1(new Foo());
auto p2 = p1;
p1.erase(); // This deletes the foo referred to by p1.
if (p2.expired())
std::cout << "p2 is expired\n"; // this will fire
Run Code Online (Sandbox Code Playgroud)
One way to achieve this with the current standard library is to make a shared_ptr to A in some scope that will exist for the lifetime of A, always refer to A by passing weak_ptrs around, and delete A when it is no longer needed by …
我想返回一个包含类似std::vector或std::unordered_map等类型的元组,其中对象可能足够大,我关心不复制。我不确定当返回的对象包装在元组中时复制省略/返回值优化将如何工作。为此,我在下面编写了一些测试代码,但对其输出的部分内容感到困惑:
#include <tuple>
#include <iostream>
struct A {
A() {}
A(const A& a) {
std::cout << "copy constructor\n";
}
A(A&& a) noexcept {
std::cout << "move constructor\n";
}
~A() {
std::cout << "destructor\n";
}
};
struct B {
};
std::tuple<A, B> foo() {
A a;
B b;
return { a, b };
}
std::tuple<A, B> bar() {
A a;
B b;
return { std::move(a), std::move(b) };
}
std::tuple<A, B> quux() {
A a;
B b;
return …Run Code Online (Sandbox Code Playgroud) 在Timur Doumler 在 CppCon 2022 的演讲“C++ Lambda Idioms”中,大约 45:19,他正在讨论 lambda 重载技巧,并说了类似的话
在 C++17 中,我们必须编写一个推导指南才能完成这项工作——这就像多了两行代码——但从 C++20 开始,我们有了[我不知道他在说什么],所以我们不必再这样做了...
为什么在 C++20 中创建 lambda 重载集不再需要演绎指南?
What is the simplest way to make a semantic action that extracts a string from a typical identifier parser based on boost::spirit::x3::lexeme?
I thought it might be possible to bypass needing to unpack the attribute and just use iterators into the input stream but apparently x3::_where does not do what I think it does.
The following yields output being empty. I expected it to contain "foobar_hello".
namespace x3 = boost::spirit::x3;
using x3::_where;
using x3::lexeme;
using x3::alpha;
auto ctx_to_string = …Run Code Online (Sandbox Code Playgroud) 可以测试三个二维点,a,b,和c,落在一条线通过注意线段(的斜率a,b)将必须是相同的(b,c),或通过注意到它们限定的三角形的面积当且仅当三个点共线时才为零。我选择了前一种方法,因为数学看起来更简洁:这个答案包含公式。
上面的问题是,虽然当我们将测试转换为代码时数学是完全正确的,但我们发现它的表现很差,因为浮点类型的基本不精确性。考虑以下 C++:
using point = std::tuple<float, float>;
bool are_collinear(point a, point b, point c, float eps) {
auto [a_x, a_y] = a;
auto [b_x, b_y] = b;
auto [c_x, c_y] = c;
auto test = (b_x - a_x) * (c_y - a_y) - (c_x - a_x) * (b_y - a_y);
return std::abs(test) < eps;
}
int main()
{
point a = { 28.8171,77.9103 …Run Code Online (Sandbox Code Playgroud) 考虑如下代码:
#include <iostream>
#include <unordered_map>
std::unordered_map<char, int> get_letter_frequencies(const std::string& str) {
std::unordered_map<char, int> freqs;
for (char ch : str) {
auto iter = freqs.find(ch);
if (iter == freqs.end()) {
freqs[ch] = 1;
} else {
iter->second++;
}
}
return freqs;
}
int main()
{
std::string str = "AABBDBCABDA";
auto freqs = get_letter_frequencies(str);
std::cout << freqs['B'] << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它存储 unordered_map 中的字母计数。我的问题是有一段简洁/更惯用的代码可以用来替换
auto iter = freqs.find(ch);
if (iter == freqs.end()) {
freqs[ch] = 1;
} else {
iter->second++;
} …Run Code Online (Sandbox Code Playgroud) 最近我写了一些类似于以下的代码
namespace Foo
{
public struct Bar
{
public float val;
public Bar(float val) { this.val = val; }
}
internal class Program
{
static void Main(string[] args)
{
int rows = 3;
int cols = 4;
List<List<Bar>> mat = Enumerable.Repeat(
Enumerable.Repeat(default(Bar), cols).ToList(),
rows
).ToList();
foreach(var i in Enumerable.Range(0, rows * cols))
{
int row = i / cols;
int col = i % cols;
mat[row][col] = new Bar((float)i);
}
foreach (var row in Enumerable.Range(0, rows))
{
foreach (var col …Run Code Online (Sandbox Code Playgroud) c++ ×6
boost-spirit ×1
c# ×1
c++11 ×1
c++20 ×1
ctad ×1
linq ×1
template-argument-deduction ×1
tuples ×1