鉴于此代码示例,有关传递给临时字符串的生命周期的规则是什么S.
struct S
{
// [1] S(const std::string& str) : str_{str} {}
// [2] S(S&& other) : str_{std::move(other).str} {}
const std::string& str_;
};
S a{"foo"}; // direct-initialization
auto b = S{"bar"}; // copy-initialization with rvalue
std::string foobar{"foobar"};
auto c = S{foobar}; // copy-initialization with lvalue
const std::string& baz = "baz";
auto d = S{baz}; // copy-initialization with lvalue-ref to temporary
Run Code Online (Sandbox Code Playgroud)
根据标准:
N4140 12.2 p5.1(在N4296中删除)
绑定到构造函数的ctor-initializer(12.6.2)中的引用成员的临时绑定将持续存在,直到构造函数退出.
N4296 12.6.2 p8
绑定到mem-initializer中的引用成员的临时表达式是错误的.
因此,拥有用户定义的构造函数[1]绝对不是我们想要的.它甚至应该在最新的C++ 14中形成不良(或者是它?)gcc和clang都没有警告它.
它是否随直接聚合初始化而改变?在这种情况下,我看起来,临时寿命延长了.
现在关于复制初始化,默认移动构造函数和引用成员状态[2] …
我一直在学习RabbitMQ各种拓扑,但是,我找不到任何对生产者发出的动态队列创建(也就是Declare Queue)的引用.想法是根据特定事件(例如HTTP请求)动态创建队列.队列将是临时的,具有TTL集并以事件ID命名.然后,消费者可以订阅主题"event.*"并合并与其相关的所有消息.
例:
现在,RabbitMQ的这种情况是否可行?
与常规递归函数相比,递归lambda函数是否会引发任何开销(因为我们必须将它们捕获到std :: function中)?
这个函数与仅使用常规函数的类似函数有什么区别?
int main(int argc, const char *argv[])
{
std::function<void (int)> helloworld = [&helloworld](int count) {
std::cout << "Hello world" << std::endl;
if (count > 1) helloworld(--count);
};
helloworld(2);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我对无限制工会及其在实践中的应用有一些疑问。假设我有以下代码:
struct MyStruct
{
MyStruct(const std::vector<int>& a) : array(a), type(ARRAY)
{}
MyStruct(bool b) : boolean(b), type(BOOL)
{}
MyStruct(const MyStruct& ms) : type(ms.type)
{
if (type == ARRAY)
new (&array) std::vector<int>(ms.array);
else
boolean = ms.boolean;
}
MyStruct& operator=(const MyStruct& ms)
{
if (&ms != this) {
if (type == ARRAY)
array.~vector<int>(); // EDIT(2)
if (ms.type == ARRAY)
new (&array) std::vector<int>(ms.array);
else
boolean = ms.boolean;
type = ms.type;
}
return *this;
}
~MyStruct()
{
if (type == ARRAY)
array.~vector<int>();
}
union { …Run Code Online (Sandbox Code Playgroud) 我有以下情况需要从t1移动构造t2.不幸的是,我不可能这样做(我认为
是constness违规)从foo的调用者那里透明地处理它的正确方法是什么?(即不需要传递值和显式的std :: move)
struct T
{
T() = default;
~T() = default;
T(T&&) = default;
};
T foo(const T& t)
{
T t3;
if (predicate)
return t3;
else
return std::move(t);
}
int main()
{
T t1;
T t2 = foo(t1);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个项目,我想在C++应用程序中加载Go插件.
经过大量的研究,我不清楚Go是否支持这一点.我遇到了很多讨论指出动态链接的坏习惯,而不是IPC.此外,我不清楚语言是否打算动态链接(新的Go哲学?).
cgo提供了从C(Go内部)调用Go或Go调用C的能力,但不是来自普通C语言.或者是吗?
显然,上游也会发生一些事情(https://codereview.appspot.com/7304104/)
main.c中
extern void Print(void) __asm__ ("example.main.Print");
int main() {
Print();
}
Run Code Online (Sandbox Code Playgroud)
print.go
package main
import "fmt"
func Print() {
fmt.Printf("hello, world\n")
}
Run Code Online (Sandbox Code Playgroud)
Makefile:
all: print.o main.c
gcc main.c -L. -lprint -o main
print.o: print.go
gccgo -fno-split-stack -fgo-prefix=example -fPIC -c print.go -o print.o
gccgo -shared print.o -o libprint.so
Run Code Online (Sandbox Code Playgroud)
输出:
/usr/lib/libgo.so.3: undefined reference to `main.main'
/usr/lib/libgo.so.3: undefined reference to `__go_init_main'
Run Code Online (Sandbox Code Playgroud)
有解决方案吗?什么是最好的方法?分叉+ IPC?
参考文献: