考虑一下:
#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返回后最终会有一个悬空引用,或者只要原始引用的对象仍然存在,捕获的引用是否可以保证工作?
在这里寻找明确的基于标准的答案:) 到目前为止它在实践中运作良好;)
以下代码:
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++ 吗?
我的猜测是它应该是有效的,因为 …
我目前正在使用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 ++代码创建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的函数,该函数也将字符串的长度作为参数?
请考虑以下代码:
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中执行此操作的(优雅)方式是什么?
想象一下我有两个不同的翻译单元 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) 这是我执行以下代码后得到的结果:
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远小于第二行或第三行?
我知道我可以使用错误代码async_write,async_read检查断线。
但是,在我的情况下,收到东西后,我不能立即回信(或者回信可能永远不会发生)。
在读写之间,如何查看客户端掉线异常?
我想创建一个多态对象数组,其中的构造函数根据其动态类型采用不同的虚拟参数。在阅读了有关用户定义和结构构造函数的内容后,我发现无法将这些概念应用于动态分配的对象。拥有 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) 假设我有一个扩展(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++,我在堆内存上工作,在那里我必须删除我使用“new”关键字在堆上创建的堆内存,我总是很困惑如何在 python 中为堆内存做什么来阻止内存泄漏推荐我任何有关 python 内存分配和删除细节的文本。谢谢
下面我的复制构造函数工作正常,但我不明白我的复制赋值运算符有什么问题.
#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++ ×10
templates ×3
c++11 ×2
android ×1
android-ndk ×1
boost ×1
boost-beast ×1
c++14 ×1
c++17 ×1
constexpr ×1
constructor ×1
folly ×1
fortran ×1
heap-memory ×1
inheritance ×1
java ×1
lambda ×1
linker ×1
memory-leaks ×1
oop ×1
polymorphism ×1
python ×1
sfinae ×1
void-t ×1
websocket ×1