小编Jod*_*cus的帖子

在C++ 11 lambda中通过引用捕获引用

考虑一下:

#include <functional>
#include <iostream>

std::function<void()> make_function(int& x) {
    return [&]{ std::cout << x << std::endl; };
}

int main() {
    int i = 3;
    auto f = make_function(i);
    i = 5;
    f();
}
Run Code Online (Sandbox Code Playgroud)

是否保证在5不调用未定义行为的情况下输出该程序?

我理解如果我x通过value([=])捕获它是如何工作的,但我不确定我是否通过引用捕获它来调用未定义的行为.可能是我在make_function返回后最终会有一个悬空引用,或者只要原始引用的对象仍然存在,捕获的引用是否可以保证工作?

在这里寻找明确的基于标准的答案:) 到目前为止它在实践中运作良好;)

c++ lambda language-lawyer c++11

59
推荐指数
2
解决办法
2万
查看次数

类范围内的 constexpr 变量的初始值设定项是否允许引用该变量?

以下代码:

struct S {
    static constexpr int rolling_sum[4]{
        0,
        rolling_sum[0] + 1,
        rolling_sum[1] + 2,
        rolling_sum[2] + 3
    };
};
Run Code Online (Sandbox Code Playgroud)

被 clang 接受(用版本 12 测试),但被 gcc(用版本 11 测试)拒绝,并出现以下错误:

test.cpp:4:9: error: ‘rolling_sum’ was not declared in this scope
    4 |         rolling_sum[0] + 1,
      |         ^~~~~~~~~~~
test.cpp:5:9: error: ‘rolling_sum’ was not declared in this scope
    5 |         rolling_sum[1] + 2,
      |         ^~~~~~~~~~~
test.cpp:6:9: error: ‘rolling_sum’ was not declared in this scope
    6 |         rolling_sum[2] + 3
      |         ^~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

这段代码是有效的 C++ 吗?

我的猜测是它应该是有效的,因为 …

c++ language-lawyer constexpr c++14

18
推荐指数
2
解决办法
270
查看次数

来自并发哈希映射的迭代器是否安全?

我目前正在使用Facebook的并发哈希映射,我想知道这样的事情是否可行:

folly::ConcurrentHashMap<std::string, some_class> m;

// add some elements

const auto it = m.find("a");

// during this time, another thread removes the "a" element

if (it != m.end())
    it->second.something(); // it is now an invalid iterator
Run Code Online (Sandbox Code Playgroud)

在阅读了哈希映射的源代码后,我发现了这个:

迭代器持有返回元素的危险指针.只有在迭代器仍然有效时才能访问元素!

这是非常令人不安的,感觉使用任何返回的迭代器是不安全的,这是这样吗?

c++ multithreading folly

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

将std :: string转换为ndk jstring

我正在尝试为现有的C ++代码创建Java接口。我的功能之一与此相似:

JNIEXPORT jstring JNICALL Java_com_testproxy_NativeInterface_serialize
    (JNIEnv* env, jobject obj, along op)
{
     return env -> NewStringUTF(<somestdstring>.c_str());
}
Run Code Online (Sandbox Code Playgroud)

问题在于,在某些情况下,第一个元素为'\ 0',因此,返回值是一个空字符串。那么是否有一些将char *转换为jstring的函数,该函数也将字符串的长度作为参数?

c++ java android android-ndk

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

使用另一列作为条件过滤一列

我想使用 A 列作为条件来过滤 B 列。我正在处理大量数据。B 列由近 90.000 行组成。我尝试过使用高级过滤功能。但我在让它发挥作用时遇到了一些问题。任何提示将不胜感激。

在此输入图像描述

excel-formula

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

混合void_t和可变参数模板?

请考虑以下代码:

template <class F, class... Args, class = std::void_t<>>
struct is_invokable
: std::false_type {};
template <class F, class... Args>
struct is_invokable<F, Args..., std::void_t<std::invoke_result_t<F, Args...>>>
: std::true_type {};
Run Code Online (Sandbox Code Playgroud)

目标是有一个特性,它能够判断类型F的可调参数是否可以使用类型的参数调用Args....

但是,它无法编译,因为:

error: parameter pack 'Args' must be at the end of the template parameter list
Run Code Online (Sandbox Code Playgroud)

在C++ 17中执行此操作的(优雅)方式是什么?

c++ sfinae variadic-templates c++17 void-t

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

模板定义和 ODR 冲突

想象一下我有两个不同的翻译单元 a.cpp 的情况

#include <iostream>

double bar();

template <typename T>
T foobar(T t) {
    return t;
}

int main() {
    std::cout << "foobar called from b.cpp: " << bar() << '\n';
    std::cout << "foobar called from a.cpp: " << foobar(1.) << '\n';
}
Run Code Online (Sandbox Code Playgroud)

和b.cpp:

template <typename T>
T foobar(T t) {
    return t + 1.;
}

double bar() {
    return foobar(1.);
}
Run Code Online (Sandbox Code Playgroud)

我知道对于模板,ODR 有例外,即编译器将这样标记实例化的函数模板,并在链接过程中删除除一个之外的所有模板。我注意到编译器实际上并不关心不同翻译单元的此类实例化生成的代码是否实际上相同或至少等效。

上面的代码就是这种情况。编译、链接和运行时

c++ a.cpp b.cpp -o result -std=c++17 && ./result
Run Code Online (Sandbox Code Playgroud)

它将产生结果

foobar called from b.cpp: 1
foobar …
Run Code Online (Sandbox Code Playgroud)

c++ linker templates one-definition-rule language-lawyer

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

为什么c ++中的"\ t"有不同的宽度?

这是我执行以下代码后得到的结果:

标签结果

cout << "The size of an int is: \t" << sizeof(int) << " bytes.\n";
cout << "The size of an long is: \t" << sizeof(long) << " bytes.\n";
cout << "The size of an double is: \t" << sizeof(double) << " bytes.\n";
Run Code Online (Sandbox Code Playgroud)

为什么第一行中的\ t远小于第二行或第三行?

c++

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

如何使用 boost::beast 检测 websockt 中的断开连接?

我知道我可以使用错误代码async_writeasync_read检查断线。

但是,在我的情况下,收到东西后,我不能立即回信(或者回信可能永远不会发生)。

在读写之间,如何查看客户端掉线异常?

c++ boost websocket boost-beast

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

Fortran 中分配和构造多态对象的规范方法是什么?

我想创建一个多态对象数组,其中的构造函数根据其动态类型采用不同的虚拟参数。在阅读了有关用户定义和结构构造函数的内容后,我发现无法将这些概念应用于动态分配的对象。拥有 C++ 背景,我习惯了在动态或在堆栈上分配对象时可以使用同一个构造函数“成员函数”的概念,但如何在分配的对象上显式调用用户定义的 Fortran 构造函数呢?

相反,我尝试摆弄通用和类型绑定的初始化函数:

module mod
type :: basis_t
contains
    procedure, public :: init_func => init_base
    ! I want a generic constructor function
    generic, public   :: init => init_func 
end type

type, extends(basis_t) :: extended_t
contains
    ! cannot work, init_extended has a different signature from init_base
    procedure, public :: init => init_extended 
end type

type wrapper_t
   type(basis_t), pointer :: obj
end type

contains
   subroutine init_base(this)
      class(base_t), intent(inout) :: this
   end subroutine

   subroutine init_extended(this, param)
      class(extended_t), intent(inout) :: this …
Run Code Online (Sandbox Code Playgroud)

oop polymorphism constructor fortran

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

如何防止 ADL 期间出现阴影?

假设我有一个扩展(STL)容器的类并提供了一个习惯的begin成员函数:

#include <vector>

template <typename Cont>
struct Bar {
    Cont c;
    auto my_begin() { return begin(c); }
};

int main() {
    Bar<std::vector<int>> b;
    b.my_begin();
}
Run Code Online (Sandbox Code Playgroud)

通过ADL,我不必std::begin()调用前指定。这很好,因为std::begin(v)总是尝试调用v.begin(),用户可能想要使用没有.begin()接口的自定义容器,因此如果他定义了自己的自由函数begin(v)Bar就会使用它。但是,如果我也重命名my_begin为ADL,似乎 ADL 将不再起作用begin,就好像它被掩盖了一样。编译器只会抱怨它在类范围内找不到匹配的调用:

prog.cc: In instantiation of 'auto Bar<Cont>::begin() [with Cont = std::vector<int>]':
prog.cc:11:13:   required from here
prog.cc:6:27: error: use of 'auto Bar<Cont>::begin() [with Cont = std::vector<int>]' before deduction of 'auto'
    6 |     auto begin() …
Run Code Online (Sandbox Code Playgroud)

c++ templates argument-dependent-lookup

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

如何在Python中释放堆内存

我来自 C++,我在堆内存上工作,在那里我必须删除我使用“new”关键字在堆上创建的堆内存,我总是很困惑如何在 python 中为堆内存做什么来阻止内存泄漏推荐我任何有关 python 内存分配和删除细节的文本。谢谢

python memory-leaks heap-memory

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

具有多重继承的复制赋值运算符

下面我的复制构造函数工作正常,但我不明白我的复制赋值运算符有什么问题.

#include <iostream>

template <typename... Ts> class foo;

template <typename Last>
class foo<Last> {
    Last last;
public:
    foo (Last r) : last(r) { }
    foo() = default;
    foo (const foo& other) : last(other.last) { }

    foo& operator= (const foo& other) {
        last = other.last;
        return *this;
    }
};

template <typename First, typename... Rest>
class foo<First, Rest...> : public foo<Rest...> {
    First first;
public:
    foo (First f, Rest... rest) : foo<Rest...>(rest...), first(f) { }
    foo() = default;
    foo (const foo& other) …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates assignment-operator c++11

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