不言自明。
基本上,假设我有这样的类型列表:
using type_list_1 = type_list<int, somestructA>;
using type_list_2 = type_list<somestructB>;
using type_list_3 = type_list<double, short>;
Run Code Online (Sandbox Code Playgroud)
它们可以是可变数量的类型列表。
如何获得笛卡尔积的类型列表?
result = type_list<
type_list<int, somestructB, double>,
type_list<int, somestructB, short>,
type_list<somestructA, somestructB, double>,
type_list<somestructA, somestructB, short>
>;
Run Code Online (Sandbox Code Playgroud)
我确实涉足了如何创建这里给出的双向笛卡尔积:如何创建类型列表的笛卡尔积?,但 n 方式似乎没有那么微不足道。
目前我正在尝试...
using type_list_1 = type_list<int, somestructA>;
using type_list_2 = type_list<somestructB>;
using type_list_3 = type_list<double, short>;
Run Code Online (Sandbox Code Playgroud)
我只想说,考虑到正确处理的难度,只需使用巴里的答案中的 boost 即可。不幸的是,我不得不坚持使用手动方法,因为是否使用 boost 是来自其他地方的决定:(
#include <iostream>
#include <string>
void foo(int& k) { std::cout << "int&\n"; }
void foo(int&& k) { std::cout << "int&&\n"; }
void foo(const int& k) { std::cout << "const int&\n"; }
void foo(const int&& k) { std::cout << "const int&&\n"; }
int main() {
static constexpr int k = 1;
foo(k);
foo(1);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
const int&
int&&
Run Code Online (Sandbox Code Playgroud)
constexpr变量到底是什么?foo给出的重载const int&.
编辑:继续使用constexpr推断为const T&;
为什么类范围的constexpr无法传递给采用通用引用的函数?!
#include <type_traits>
template <typename T>
void goo(T&& k) {
static_assert(std::is_same<decltype(k), const int&>::value, "k …Run Code Online (Sandbox Code Playgroud) 考虑下面的情况
名称字符串作为参数移动到线程.
void start(std::string&& name) {
t = std::thread{&ThreadRunner::run, this, std::forward<std::string>(name)};
}
Run Code Online (Sandbox Code Playgroud)
线程的run函数也采用右值引用.
void run(std::string&& name) {
const auto errnum = pthread_setname_np(t.native_handle(), name.c_str());
if (errnum != 0) {
std::cout << "ERROR " << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
线程通过start函数创建,如下所示:
ThreadRunner r;
r.start(std::string("somename"));
Run Code Online (Sandbox Code Playgroud)
问题是.run通过pthread_setname_np 在函数中访问的std :: string是否可能是垃圾,因为当范围结束时临时超出了范围?
演示
在上面的演示中,在call结束之后,是否保证somename字符串在函数中有效run?
编辑:
使用构造函数/析构函数进行演示
问题中的std :: string现在替换为a Wrap来打印所涉及的构造函数.
结果是:(第二个字段是对象的地址,第三个是线程ID)
Constructed 0x7ffc395e0ef0 140605000369984
Move Constructed 0x7ffc395e0e60 140605000369984
Move Constructed 0x177a0a0 140605000369984
Destroyed 0x7ffc395e0e60 140605000369984
Destroyed 0x7ffc395e0ef0 140605000369984
Call …Run Code Online (Sandbox Code Playgroud) std::tuple a{1,3,4,5} -> 使其成为大于 3 的数字
std::tuple b{4,5}
Run Code Online (Sandbox Code Playgroud)
或者
std::tuple a{
std::integral_constant<int,1> {},
std::integral_constant<int,3> {},
std::integral_constant<int,4> {},
std::integral_constant<int,5> {}
}
Run Code Online (Sandbox Code Playgroud)
到
std::tuple a{
std::integral_constant<int,4>{},
std::integral_constant<int,5>{}
};
Run Code Online (Sandbox Code Playgroud)
如何在编译时转换它?我可以使用integer_sequence它来做到这一点,但这很麻烦。在 C++17 中是否有更简单的方法使用折叠表达式或std::apply
同样在过滤器之后,还需要获取唯一条目的元组。但我的假设是,如果可以进行过滤,那么找到唯一值将是微不足道的。
编辑以便更清楚:
std::tuple<int_c<1>, int_c<3>,int_c<4>,int_c<5>> to std::tuple<int_c<4>,int_c<5><-- 如果可以在没有额外声明函数的情况下以简洁的 c++17 方式实现,那就可以了!
编辑:我在摆弄,也许这样的事情会奏效:
与template... C作为积分常数列表:
constexpr auto result = std::tuple_cat(std::conditional_t<(C::value > 3), std::tuple<C>, std::tuple<>>{}...);
Run Code Online (Sandbox Code Playgroud) 在这种情况下,两个负载会合二为一吗?如果这是依赖于架构的,那么说英特尔的现代处理器会是什么情况?我相信原子负载相当于英特尔处理器中的正常负载。
void run1() {
auto a = atomic_var.load(std::memory_order_relaxed);
auto b = atomic_var.load(std::memory_order_relaxed);
// Some code using a and b;
}
void run2() {
if (atomic_var.load(std::memory_order_relaxed) == 2 && /*some conditions*/ ...) {
if (atomic_var.load(std::memory_order_relaxed) * somevar > 3) {
/*...*/
}
}
}
Run Code Online (Sandbox Code Playgroud)
run1()并且run2()只是使用相同原子变量的两个负载的两个场景。编译器能否将这种两种加载的场景合并为一种加载并重用它?
我应该将我创建的所有画笔或直接指定为冻结的背景吗?我不修改的其他用户控件怎么样?
Freezable 提供 Changed 事件来通知观察者对对象的任何修改。冻结 Freezable 可以提高其性能,因为它不再需要在更改通知上花费资源。冻结的 Freezable 还可以跨线程共享,而未冻结的 Freezable 则不能。
由此看来,如果我的应用程序从未对创建的画笔进行任何更改,那么无论我是否设置冻结它,性能方面都没有差异。我对么?
A::thread是由main线程创建的.我可以加入A::thread线程goo吗?
struct A {
std::thread thread;
void foo() {
thread=std::thread{[]() { sleep(10); }};
}
};
void goo(A& a) {
a.thread.join();
}
int main() {
A a;
a.foo();
std::thread other_thread{goo, a};
other_thread.join();
};
Run Code Online (Sandbox Code Playgroud) struct Data {
double a;
double b;
double c;
};
Run Code Online (Sandbox Code Playgroud)
如果在不同的线程上读取,但只有一个其他线程正在写入 a、b、c 中的每一个,那么读取每个双精度值是否正常?
如果我确保Data对齐,会出现什么情况?
struct Data {double a,b,c; } __attribute__((aligned(64));
Run Code Online (Sandbox Code Playgroud)
这将确保 a,b,c 中的每一个都对齐到 64,64+8, 64+16... 所以总是对齐到 8*8=64 位边界。
这个问题对原子 x86 指令的对齐要求及其答案让我认为Data::a/b/c从另一个线程写入并同时读取它们而不使用std::atomic.
是的,我知道std::atomic会解决这个问题,但这不是问题。
正常if情况下,短路工作。
但是,如果尝试短路 if-constexpr 不起作用:
#include <iostream>
template <typename ... Args>
void foo(Args... args) {
std::string a;
// for the call of foo, sizeof...(args) = 0, so a > 2 shouldn't be evaluated.
if constexpr (sizeof...(args) == 0 || a > 2) {
std::cout << "ASD" << '\n';
}
}
int main() {
foo();
}
Run Code Online (Sandbox Code Playgroud)
编辑:似乎很多评论都与我的尝试有所不同。我将引用@chris 的评论:
人们似乎没有抓住重点,所以这里有一个更好的例子来说明为什么这很有用:
Run Code Online (Sandbox Code Playgroud)if constexpr (sizeof...(Ts) > 0 && is_integral_v<first_t<Ts...>>) { /* corresponding logic */ }目前,这需要嵌套的 constexpr ifs
这似乎目前是不可能的,唯一的解决方法是编写嵌套的 ifs。