小编jwe*_*rek的帖子

是否可以在标准弱指针之上实现非所有者的“稍微聪明”的指针?

我一直在思考,虽然我了解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 …

c++ smart-pointers

3
推荐指数
2
解决办法
138
查看次数

在 C++ 中返回多个大对象的最佳方法是什么?

我想返回一个包含类似std::vectorstd::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)

c++ tuples move-semantics

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

为什么在 C++20 中不再需要使用推导指南来创建 lambda 重载集?

Timur Doumler 在 CppCon 2022 的演讲“C++ Lambda Idioms”中,大约 45:19,他正在讨论 lambda 重载技巧,并说了类似的话

在 C++17 中,我们必须编写一个推导指南才能完成这项工作——这就像多了两行代码——但从 C++20 开始,我们有了[我不知道他在说什么],所以我们不必再这样做了...

为什么在 C++20 中创建 lambda 重载集不再需要演绎指南?

c++ template-argument-deduction c++20 ctad

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

How do you get a string out of a Boost Spirit X3 lexeme parser?

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)

c++ boost-spirit boost-spirit-x3

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

以数值稳健的方式测试三点的共线性

可以测试三个二维点,ab,和c,落在一条线通过注意线段(的斜率ab)将必须是相同的(bc),或通过注意到它们限定的三角形的面积当且仅当三个点共线时才为零。我选择了前一种方法,因为数学看起来更简洁:这个答案包含公式

上面的问题是,虽然当我们将测试转换为代码时数学是完全正确的,但我们发现它的表现很差,因为浮点类型的基本不精确性。考虑以下 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)

c++ numerical-methods computational-geometry

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

是否有更惯用的方法来“插入或累积”到表示项目计数的 unordered_map 中?

考虑如下代码:

#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)

c++ unordered-map c++11

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

如何使用 LINQ 在 C# 中填充列表的列表?

最近我写了一些类似于以下的代码

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# linq

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