auto

A lambda's return type can be deduced by the return value, so why can't a function's?

走远了吗. 提交于 2019-11-28 17:18:06
问题 #include <iostream> int main(){ auto lambda = [] { return 7; }; std::cout << lambda() << '\n'; } This program compiles and prints 7. The return type of the lambda is deduced to the integer type based on the return value of 7. Why isn't this possible with ordinary functions? #include <iostream> auto function(){ return 42; } int main(){ std::cout << function() << '\n'; } error: ‘function’ function uses ‘auto’ type specifier without trailing return type 回答1: C++14 has this feature. You can test

6.变量的生存期

青春壹個敷衍的年華 提交于 2019-11-28 16:20:18
定义:变量的生存期是指其在程序运行时占用内存空间的时间段。 1. 静态生存期 从程序开始执行时分配到程序结束时收回。 如: 全局变量 2. 自动生存期 从程序执行到定义它们的复合语句时分配到复合语句执行完毕时收回。 如: 局部变量 、 函数的参数 3. 动态生存期 用new操作或者malloc函数来进行分配,用delete操作或者调用free函数来收回。 注: 1. 在定义 局部变量 时,使用auto、static、register来显式指出它们的生存期。 auto: 默认,自动生存期 static: 静态生存期,在函数多次调用时,可以 保留上次调用的结果 。 register: 自动生存期,与auto不同在于, 建议(不是一定) 编译程序把该局部变量的空间分配在CPU寄存器中,其目的是为了提高对局部变量的访问效率。 2. 内存空间分为四个部分: 静态数据区 、 代码区 、 栈区 、 堆区 静态数据区 : 全局变量 、 static存储类的局部变量 、 常量 代码区 : 函数代码 栈区 : auto存储类的局部变量 、函数的 形参 、函数调用时的有关信息(如 返回值 ) 堆区 : 动态变量 静态数据区和代码区的大小是固定的,而栈区和堆区的大小随程序的运行不断变化(不过操作系统会对其空间最大值有一定的限制)。 3. 编译程序隐式自动将静态生存期的变量 按位模式初始化为0

Is there a downside to declaring variables with auto in C++?

不羁的心 提交于 2019-11-28 16:16:05
It seems that auto was a fairly significant feature to be added in C++11 that seems to follow a lot of the newer languages. As with a language like Python, I have not seen any explicit variable declaration (I am not sure if it is possible using Python standards). Is there a drawback to using auto to declare variables instead of explicitly declaring them? You've only asked about drawbacks, so I'm highlighting some of those. When used well, auto has several advantages as well. The drawbacks result from ease of abuse, and from increased potential for code to behave in unintended ways. The main

The relationship between auto and decltype

ぐ巨炮叔叔 提交于 2019-11-28 16:06:01
Is auto x = initializer; equivalent to decltype(initializer) x = initializer; or decltype((initializer)) x = initializer; or neither? decltype also considers whether the expression is rvalue or lvalue . Wikipedia says , The type denoted by decltype can be different from the type deduced by auto. #include <vector> int main() { const std::vector<int> v(1); auto a = v[0]; // a has type int decltype(v[0]) b = 1; // b has type const int&, the return type of // std::vector<int>::operator[](size_type) const auto c = 0; // c has type int auto d = c; // d has type int decltype(c) e; // e has type int,

ios input框取不到光标不能输入

不羁岁月 提交于 2019-11-28 15:56:15
-webkit-user-select :none ; 在移动端开发中,我们有时有针对性的写一些特殊的重置,比如: * { -webkit - touch - callout: none; //-webkit-touch-callout:none; 阻止长按图片之后呼出菜单提示复制的行为 //禁用Webkit内核浏览器的文字大小调整功能。 -webkit-text-size-adjust: none; //避免点击a标签或者注册了click事件的元素时产生高亮 -webkit-tap-highlight-color: rgba(0, 0, 0, 0); // //禁止用户进行复制.选择 . -webkit-user-select: none; } 其中,-webkit-user-select :none ;会产生一些问题。 这是webkit内核浏览器下的一个bug,具体可以参考这篇文章: https://bugs.webkit.org/show_bug.cgi?id=82692 阻止了用户的选择内容行为,会导致一些“内容可编辑”标签无法正常使用,比如input、testarea。 如果网站不需要阻止用户的选择内容的行为就可以使用如下样式: { -webkit-user-select: text; -user-select: text; } 另一种方式: *: not(input,

C++ auto& vs auto

余生颓废 提交于 2019-11-28 15:31:29
When creating local variables, is it correct to use (const) auto& or auto ? e.g.: SomeClass object; const auto result = object.SomeMethod(); or const auto& result = object.SomeMethod(); Where SomeMethod() returns a non-primitive value - maybe another user-defined type. My understanding is that const auto& result is correct since the result returned by SomeMethod() would call the copy constructor for the returned type. Please correct me if I am wrong. What about for primitive types? I assume const auto sum = 1 + 2; is correct. Does this also apply to range based for loops? for(const auto&

css实现水平垂直居中

空扰寡人 提交于 2019-11-28 15:20:22
第一种:利用css3的transform和绝对定位 <!DOCTYPE html> <html> <head> <title></title> </head> <style type="text/css"> /*消除行内元素的默认边距*/ *{ margin: 0; padding: 0; box-sizing: border-box;/*为元素指定的任何内边距和边框都将在已设定的宽度和高度内进行绘制*/ } body{ background-color: #999; } main{ background-color: #fff; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);/*以左上角为圆点向上和左偏移自身宽高50%的距离*/ -webkit-transform: translate(-50%,-50%); -moz-transform: translate(-50%,-50%); } </style> <body> <main>水平垂直居中</main> </body> </html> 效果如图所示,并且不管如何改变页面的宽高,元素始终水平垂直居中。 第二种:利用css3的transform和相对定位 使用这种方法必须设置html,body的高度 <!DOCTYPE html

`auto` return type in context of class members

白昼怎懂夜的黑 提交于 2019-11-28 12:25:45
How can automatic type deduction be used for class members? For example, the following code struct A { auto foo(); // foo is defined in another file }; int main() { A a; a.foo(); } where foo has the return type auto results in the following error: error: function 'foo' with deduced return type cannot be used before it is defined a.foo(); ^ The error is comprehensible since the compile cannot know what foo 's return type is without knowing its definition. My question is, if there is any workaround or some kind of programming pattern to circumvent the problem that auto return type cannot be used

Why does g++5 deduces object instead of initializer_list in auto type deduction

感情迁移 提交于 2019-11-28 12:17:24
I recently came upon this code: struct Foo{}; int main() { Foo a; // clang++ deduces std::initializer_list // g++5.1 deduces Foo auto b{a}; a = b; } It compiles fine with g++5.1, but fails in clang++ (used both -std=c++11 and -std=c++14 , same results). The reason is that clang++ deduces the type of b as std::initializer_list<Foo> , whereas g++5.1 deduces as Foo . AFAIK, the type should indeed be (counter-intuitive indeed) std::initializer_list here . Why does g++5 deduces the type as Foo ? There is a proposal for C++1z that implements new type deduction rules for brace initialization ( N3922

SFINAE and decltype(auto)

家住魔仙堡 提交于 2019-11-28 10:48:55
If a function template returns decltype(auto) (or another type specifier using auto ) but the return statement would be ill-formed, does SFINAE result? Is the return statement considered to be the immediate context of the function signature? Nothing in the N3690 draft seems to require this. By default, I guess SFINAE does not apply. This seems unfortunate because you can write a function to forward to another function, but you cannot make its existence conditional on the delegate as when writing longhand. Furthermore, checking the existence of a peer nonstatic member function cannot be done