1. decay
std::decay对类型进行退化处理。
a. T为数组U或数组U引用,则type为U*.
b. T为函数时,则type为std::add_pointer<F>::type.
c. 其它类型则移除cv限定符(const和volatile),则type为std::remove_cv<std::remove_reference<T>::type>::type.
decay_equiv<const int&, int>::value // true decay_equiv<int[2], int*>::value // true decay_equiv<int(int), int(*)(int)>::value // true
2. decltype
delctype 可以在编译期内推导表达式所得值的类型。
template<typename T1, typename T2> void sum(T1 &t1, T2 &t2, decltype(t1 + t2) &s) { s = t1 + t2; } template<typename T1, typename T2> auto sum(T1 &t1, T2 &t2) ->decltype(t1 + t2)) { return t1 + t2; } int hash(char*); map<string, decltype(hash(nullptr))> mm; // 动态指定hash()返回类型,方便后续维护
3. declval
std::declval 返回对象的右值引用,不管对象是否有构造函数,一般配合decltype使用。
struct MyOb { MyObj1(){ cout << "Constuctor" << endl; } int foo() const { return 1; } }; struct MyObj { MyObj(const MyObj&){} int foo() const { return 1; } }; void test() { decltype(MyOb().foo()) n0 = 1; // ok, 构造函数不会调用 decltype(MyObj().foo()) n1 = 1; // 编译错误,MyObj没有默认构造函数 decltype(std::declval<MyObj>().foo()) n2 = 2; // ok, int cout << typeid(n2).name() << endl; // i }
3. 元素分组实例
定义Person结构,如按照人物姓名,年龄,城市各个不同的维度进行分组,可很快写出以下版本:
struct Person { string name; int age; string city; }; template<typename T, typename Fn> multimap<T, Person> GroupBy(const vector<Person>& vt, const Fn& keyfn) { multimap<T, Person> map; std::for_each(vt.begin(), vt.end(), [&map, $keyfn](const Person& person) { map.insert({keyfn(person), person}); // keyfn返回值为key }); return map; }
传统方式通过显式指定key类型,可通过c++11语法推导key类型:
方式一:declval
multimap<decltype(declval<Fn>()(declval<Person>())), Person> mp;
方式二. result_of
multimap<typename std::result_of<Fn(Person)>::type, Person> mp;
方式二使用更加简洁,用result_of改写上述版本:
typedef typename vector<Person>::value_type value_type; template<typename Fn> multimap<typename result_of<Fn(value_type)>::type, value_type> GroupBy(const vector<Person>& vt, const Fn& keyfn) { typedef typename result_of<Fn(value_type)>::type keytype; multimap<keytype, value_type> map; std::for_each(vt.begin(), vt.end(), [&map, &keyfn](const Person& person) { map.insert({keyfn(person), person}); }); return map; } // 调用 multimap<int, Person> mp = GroupBy(v, [](const Person &p) { return p.age; });
cppreference可能实现:
// Minimal C++11 implementation: template <class> struct result_of; template <class F, class... ArgTypes> struct result_of<F(ArgTypes...)> { using type = decltype(detail::INVOKE(std::declval<F>(), std::declval<ArgTypes>()...)); };
参考链接:
https://en.cppreference.com/w/cpp/types/result_of
https://www.cnblogs.com/anzhsoft/p/3602984.html
相关推荐
Weight Decay超参的理解
These problems include radioactive decay, pollution indexes, digital circuits, differential equations, Internet addresses, data analysis, simulation, quality control, electrical networks, data ...
Chic_decay
matlab m文件
权重衰减(weight decay)与学习率衰减(learning rate decay)
com.elevatecumbe.decay.apk
碳14衰变为氮14是生命起源和演化的重要因素,邵明华,,有机分子中的碳14通过贝塔衰变转变为氮14。这种衰变是生命起源时核糖核酸的核苷酸合成的关键因素。在生命体的脱氧核糖核酸和核糖�
These problems include radioactive decay, pollution indexes, digital circuits, differential equations, Internet addresses, data analysis, simulation, quality control, electrical networks, data ...
Thinking Particles Tutorial - Ash Decay教程
11 NONLINEAR MODEL FOLLOWING CONTROL 217 11.1 Introduction/217 11.2 Design Concept/218 11.2.1 Reference Fuzzy Descriptor System/218 11.2.2 Twin-Parallel Distributed Compensations/219 11.2.3 The Common...
$ date = '2015-11-01 01:01:01' ; $ gravity = 1.8 ; $ votes = 100 ; $ z = 1.96 ; /** * Reddit */ $ redditHot = $ c -> redditHot ( $ decay , $ ups , $ down , $ date ); echo $ redditHot . "\n" ; /** *...
增加weight_decay并不会直接增加梯度,而是可以对模型参数进行正则化,从而间接地影响梯度。
基本语言特性使用auto结构化绑定不会发生类型退化(decay)
机器学习基石14 - 2 - Weight Decay Regularization (24-08).mp4
State of Decay 2 Juggernaut Edition v1.0-v15.1 Plus 19 Trainer 2020年4月20日 亲测可用,绿色无毒 ,淘宝上有卖的 跟这个一样 这个是免费使用 不封号 无毒 可用!游戏联网也可用!
self.learning_rate = tf.train.exponential_decay( self.initial_learning_rate, self.global_step, self.decay_steps, self.decay_rate, self.staircase = Ture, name='learning_rate') 采用了学习率衰减,...
SU(3)味违反衰变J /ψ→Ξ(1530)-Ξ++++ c.c。 用BEPCII的BESIII检测器收集的(1310.6±7.0)×106 J /ψ事件进行研究,测得的分支分数为B(J /ψ→Ξ(1530)-Ξ++++)=(3.17 ±0.02stat±0.08syst)×10−4。...
本文介绍了最后未知的两个回路对Z玻色子局部宽度和Z峰横截面的贡献的结果。 这些就是所谓的Bosonic电弱两回路校正,其中“ Bosonic”是指没有闭合费米子回路的图。 以及Z极不对称性的相应结果<...
attack/decay model for piano transcription,” in ISMIR, 2016. 本文提出了一种基于非负矩阵分解的方法,并进行了以下三个改进:(1)引入攻击和谐波衰减分量; (2)使用由这些组件共享的尖峰形音符激活; (3)...