auto

When not to use `auto&&`?

给你一囗甜甜゛ 提交于 2019-12-03 11:15:20
问题 auto&& mytup = std::make_tuple(9,1,"hello"); std::get<0>(mytup) = 42; cout << std::get<0>(mytup) << endl; Is there a copy/move involved (without RVO) when returning from make_tuple? Is it causing undefined behavior? I can both read write the universal reference. Can auto&& var = func() be used always instead of auto var = func() so that there is no copy/move? 回答1: Yes. Any return from a function that does not return a reference type may involve a copy/move. Eliding that is what RVO is about.

Use of 'auto func(int)' before deduction of 'auto' in C++14

随声附和 提交于 2019-12-03 10:50:14
问题 I have compiled following program in GCC using C++14 . #include <iostream> using namespace std; auto func(int i); int main() { auto ret = func(5); return 0; } auto func(int i) { if (i == 1) return i; else return func(i-1) + i; } But, I get the following error. In function 'int main()': 8:16: error: use of 'auto func(int)' before deduction of 'auto' auto ret = func(5); So, what am I missing here? 回答1: This is [dcl.spec.auto/11]: If the type of an entity with an undeduced placeholder type is

Can a variable be redeclared as auto that deduced to the same type? [duplicate]

陌路散爱 提交于 2019-12-03 10:18:05
This question already has answers here : Does a declaration using “auto” match an extern declaration that uses a concrete type specifier? (3 answers) Is the following allowed by the standard? #include <iostream> extern int a; auto a = 3; int main(int, char**) { std::cout << a << std::endl; return 0; } clang accepts the code. g++ complains for conflicting declaration. Its not much clear to me from the standard, but then, there is this written section 7.1.6.4 auto specifier A program that uses auto in a context not explicitly allowed in this section is ill-formed. Better read the mentioned

CSS fails to create a horizontal scroll bar

匿名 (未验证) 提交于 2019-12-03 09:02:45
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have CSS sheet which fails to create a horizontal scroll bar when the table width is greater than 800px wide. CSS looks like: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <style type="text/css"> html, body { max-width : 800px; margin-left:auto; margin-right:auto; } body { background-color:#000000; } #content { position:relative; z-index:1; } #header { position:relative; background-image: url(http://img.photobucket.com/albums/v224/Tahira/HARPALGETMENERDS4.jpg);

Generating all size k subsets of {0, 1, 2, … n-1}

匿名 (未验证) 提交于 2019-12-03 08:54:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to generate all cardinality k subsets of {0, 1, 2, ..., n-1} in C++. In Haskell, I would do: sets 0 n = [[]] sets k n = [i:s | i <- [0..n-1], s <- sets (k-1) i] Or in Python: def sets(k, n): if k == 0: return [()] return ((i,)+s for i in range(n) for s in sets(k-1, i)) So, for example, (line breaks added for clarity) ghci> sets 2 8 [[1,0], [2,0],[2,1], [3,0],[3,1],[3,2], [4,0],[4,1],[4,2],[4,3], [5,0],[5,1],[5,2],[5,3],[5,4], [6,0],[6,1],[6,2],[6,3],[6,4],[6,5], [7,0],[7,1],[7,2],[7,3],[7,4],[7,5],[7,6]] What would be the "C++ way" of

Auto Increment on Composite Primary Key - Sqlite3 + Python

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a code like this c.execute('CREATE TABLE IF NOT EXISTS base (ID INTEGER NOT NULL, col2 TEXT NOT NULL, col3 INTEGER, PRIMARY KEY(ID, col2))') This code gives me an sqlite3.IntegrityError exception even though I am very sure that I am writing the record for the first time. So, I tried c.execute('CREATE TABLE IF NOT EXISTS base (ID INTEGER, col2 TEXT, col3 INTEGER, PRIMARY KEY(ID, col2))') This inserts the row fine in the base table BUT, ID column is not auto incremented at all. What can I do? Any idea? 回答1: In sqlite, you only get

Function Templates vs. Auto Keyword

女生的网名这么多〃 提交于 2019-12-03 06:48:07
问题 Can the auto keyword in C++11 replace function templates and specializations? If yes, what are the advantages of using template functions and specializations over simply typing a function parameter as auto ? template <typename T> void myFunction(T &arg) { // ~ } vs. void myFunction(auto &arg) { // ~ } 回答1: In a nutshell, auto cannot be used in an effort to omit the actual types of function arguments, so stick with function templates and/or overloads. auto is legally used to automatically

centos7安装后,一些小优化

喜夏-厌秋 提交于 2019-12-03 04:44:16
1.设置静态ip   vim /etc/sysconfig/network-scripts/ifcfg-ens33   将BOOTPROTO=dhcp修改为BOOTPROTO=static   ONBOOT=no修改为ONBOOT=yes 新增   IPADDR=192.168.80.129   PREFIX=24    #NETMASK=255.255.255.0   GATEWAY=192.168.80.2   DNS1=114.114.114.114   DNS2=8.8.8.8 之后重启网络服务   systemctl restart network 2.关闭NetworkManager   systemctl status NetworkManager   systemctl stop NetworkManager   systemctl disable NetworkManager 3.关闭selinux   vim /etc/selinux/config   将SELINUX=enforcing注释掉,并添加SELINUX=disabled,之后重启电脑即可 4.修改主机名   hostnamectl set-hostname centos2 5.修改yum源为阿里云   cd /etc/yum.repos.d   wget http://mirrors.aliyun

Can i use auto or decltype instead trailing return type?

帅比萌擦擦* 提交于 2019-12-03 04:42:54
I find trailing return type so easy to define the return of a function that returns a complicated types e.g: auto get_diag(int(&ar)[3][3])->int(&)[3]{ // using trailing return type static int diag[3]{ ar[0][0], ar[1][1], ar[2][2] }; return diag; } auto& get_diag2(int(&ar)[3][3]){ // adding & auto because otherwise it converts the array to pointer static int diag[3]{ ar[0][0], ar[1][1], ar[2][2] }; return diag; } int main(){ int a[][3]{ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; decltype(get_diag(a)) diag{ get_diag(a) }; for (auto i : diag) std::cout << i << ", "; std::cout << std::endl; decltype(get

Are there any realistic use cases for `decltype(auto)` variables?

假如想象 提交于 2019-12-03 04:40:43
问题 Both from my personal experience and from consulting answers to questions like What are some uses of decltype(auto)? I can find plenty of valuable use cases for decltype(auto) as a function return type placeholder . However, I am seriously struggling to think of any valid (i.e. useful, realistic, valuable) use case for decltype(auto) variables. The only possibility that comes to mind is to store the result of a function returning decltype(auto) for later propagation, but auto&& could be used