c++

Multiple parameter packs — how?

非 Y 不嫁゛ 提交于 2021-02-20 10:10:15
问题 I have the following problem: #include <vector> #include <tuple> using namespace std; template< size_t... N_i, typename Ts... > class A { // ... private: std::vector<size_t> _v = { N_i... }; std::tuple<Ts...> _t; }; int main() { A<1> a; } As you can see above, I try to define multiple parameter packs as template arguments of the class A . Unfortunately, the code does not compile: error: expected nested-name-specifier before 'Ts' How can I define multiple parameter packs for this example? 回答1:

Multiple parameter packs — how?

孤者浪人 提交于 2021-02-20 10:06:51
问题 I have the following problem: #include <vector> #include <tuple> using namespace std; template< size_t... N_i, typename Ts... > class A { // ... private: std::vector<size_t> _v = { N_i... }; std::tuple<Ts...> _t; }; int main() { A<1> a; } As you can see above, I try to define multiple parameter packs as template arguments of the class A . Unfortunately, the code does not compile: error: expected nested-name-specifier before 'Ts' How can I define multiple parameter packs for this example? 回答1:

Multiple parameter packs — how?

穿精又带淫゛_ 提交于 2021-02-20 10:06:23
问题 I have the following problem: #include <vector> #include <tuple> using namespace std; template< size_t... N_i, typename Ts... > class A { // ... private: std::vector<size_t> _v = { N_i... }; std::tuple<Ts...> _t; }; int main() { A<1> a; } As you can see above, I try to define multiple parameter packs as template arguments of the class A . Unfortunately, the code does not compile: error: expected nested-name-specifier before 'Ts' How can I define multiple parameter packs for this example? 回答1:

How does using ellipses for SFINAE work?

空扰寡人 提交于 2021-02-20 08:59:25
问题 When using SFINAE to select constructor overloads in the past, I have typically used the following: template <typename T> class Class { public: template <typename U = T, typename std::enable_if<std::is_void<U>::value, int>::type=0> Class() { std::cout << "void" << std::endl; } template <typename U = T, typename std::enable_if<!std::is_void<U>::value, int>::type=0> Class() { std::cout << "not void" << std::endl; } }; However, I just came across this alternative: template <typename U = T,

苦逼程序员30岁离职创业2年有什么总结

被刻印的时光 ゝ 提交于 2021-02-20 08:58:30
闲来无事聊一聊 第一次写这种人生总结类的文章,写的不好各位见谅。 事出反常必有妖 作者在30岁10个月的时候选择离职,离开广州回家乡吃点粥,我家乡是美丽的广西XX市(避免广告),离职原因在这里简单说明以下几点,好让各位吃瓜群众可以更好,更放心的吃瓜。 身体,曾经有段日子加班+娱乐(打游戏)+照顾我家刚出生的小皇帝,一天要早上7点起晚上2点睡这样,一日三餐将就。最终累垮了身体,免疫力应该出现了问题,随便吃个烧烤就全身荨麻疹,看了好几个月医生,各种药。这个时候有意识要保养身体了,果然跟前辈说的一样,“一到30岁你就懂了”。 家庭,在广州的日子基本上真的忙起来的时候,早上7点出去,晚上11点回到家,你老婆孩子都看不到你,长此以往可想而知。忙的时候让老婆带小孩回外家小住一段时间。 父母,在广州10年回家加起来的日期可能不够3个月,有一次回家看到家里很多地方蜘蛛丝都有了,父母身体老了也各种问题,突然想起“子欲养而亲不待”。 买不起广州的房 顿悟了,努力赚钱同时也要保养好自己和家人的身心健康,家庭和谐。 就是这样跟老婆商量一下,选择辞职回家。 山雨欲来风满楼 回家前就制定了几个小目标: 改善我父母的关系,我父母由于陈年往事关系一直不好 改善我自己的家庭关系,调养好我的身体 创办自己的小公司,有自己的小事业 刚回家的日子头半个月很闲,基本是约下老朋友宵夜吹水,白天带老婆小孩吃各种小吃美食

Disturbing order of evaluation

空扰寡人 提交于 2021-02-20 08:32:08
问题 When I work with my favorite containers, I tend to chain operations. For instance, in the well-known Erase–remove idiom: v.erase( std::remove_if(v.begin(), v.end(), is_odd), v.end() ); From what I know of the order of evaluation, v.end() (on the rhs) might be evaluated before the call to std::remove_if . This is not a problem here since std::remove* only shuffle the vector without changing its end iterator. But it could lead to really surprising constructs, like for instance (demo): #include

Disturbing order of evaluation

夙愿已清 提交于 2021-02-20 08:31:35
问题 When I work with my favorite containers, I tend to chain operations. For instance, in the well-known Erase–remove idiom: v.erase( std::remove_if(v.begin(), v.end(), is_odd), v.end() ); From what I know of the order of evaluation, v.end() (on the rhs) might be evaluated before the call to std::remove_if . This is not a problem here since std::remove* only shuffle the vector without changing its end iterator. But it could lead to really surprising constructs, like for instance (demo): #include

Disturbing order of evaluation

孤街浪徒 提交于 2021-02-20 08:31:15
问题 When I work with my favorite containers, I tend to chain operations. For instance, in the well-known Erase–remove idiom: v.erase( std::remove_if(v.begin(), v.end(), is_odd), v.end() ); From what I know of the order of evaluation, v.end() (on the rhs) might be evaluated before the call to std::remove_if . This is not a problem here since std::remove* only shuffle the vector without changing its end iterator. But it could lead to really surprising constructs, like for instance (demo): #include

Fast interleave 2 double arrays into an array of structs with 2 float and 1 int (loop invariant) member, with SIMD double->float conversion?

試著忘記壹切 提交于 2021-02-20 06:50:27
问题 I have a section of code which is a bottleneck in a C++ application running on x86 processors, where we take double values from two arrays, cast to float and store in an array of structs. The reason this is a bottleneck is it is called either with very large loops, or thousands of times. Is there a faster way to do this copy & cast operation using SIMD Intrinsics? I have seen this answer on faster memcpy but doesn't address the cast. The simple C++ loop case looks like this int _iNum; const

ambiguous call to overloaded function

六月ゝ 毕业季﹏ 提交于 2021-02-20 06:44:07
问题 I have two functions: void DoSomething( const tchar* apsValue ) void DoSomething( size_t aiValue ) Now I want to pass '0' as a size_t: DoSomething(0); The compiler throws an error: "ambiguous call to overloaded function" To solve this, I can use static_cast, for instance: DoSomething(static_cast<size_t>(0)); Or simple: DoSomething(size_t(0)); Is one of them better than the other? Are there any other approaches to solve this? 回答1: It's ambiguous because 0 has type int , not size_t . It can