FAQ Database Discussion Community


auto && outside range-for

c++11,auto,rvalue-reference
Am I right in assuming that writing auto && x = ...; almost never makes sense outside the for (...) part of a range for, since if the right-hand side indeed is an rvalue, it will normally stop to exist at the semicolon and then x refers to something that...

'auto' not allowed in function prototype with Clang

c++,auto,c++14,clang++
Using Clang 3.5, 3.6, or 3.7, with the flag std=c++1y the following code does not compile : #include <iostream> auto foo(auto bar) { return bar; } int main() { std::cout << foo(5.0f) << std::endl; } The error given is : error: 'auto' not allowed in function prototype I do not...

Choose between template function and auto type deduction

c++,templates,auto,c++14,c++1z
I have a generic question about template functions versus auto type deduction for functions. For years, we have have been able to write template function : template <class T> T add(T a,T b){ return a+b; } There is a TS for using auto for function's parameters deduction auto add(auto a,auto...

Why is direct-list-initialization with auto considered bad or not preferred?

c++,c++11,initialization,auto,type-deduction
I've come into the habit of writing code with direct-list-initialization like below as it's more effective and it's very useful to prevent implicit narrowing: int i {0}; string s {""}; char c {'a'}; bool b {false}; auto num {100}; // But this?? But when it comes to the auto specifier,...

Automatically calling an init function whenever an object is used for the 1st time

c#,load,delay,auto,init
I have an object that only initializes itself with barebones data when constructed (fast), and loads itself for real (slow) when first accessed. The idea is that I'm creating a lot of these barebones objects at startup and hash them into a map, then fully load each object whenever it...

Why is my div not horizontally centered?

css,center,margin,auto
I have two divs horizontally aligned next to each other, each taking ~50% of the page width. Now if the browser window changes, my I want to keep one div centered (<div id="featured">) but it doesn't, even tho I set margin: 0 auto;) How can I keep it centered in...

What is the type of “auto var = {condition} ? 1 : 1.0” in C++11? Is it double or int?

c++,c++11,types,auto,ternary-operator
In C++11 what are the types of x and y when I write this? int main() { auto x = true ? 1 : 1.0; auto y = false ? 1 : 1.0; std::cout << x << endl; std::cout << y << endl; return 0; } ...

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

c++,c++11,auto,clang++,gcc5
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...

Is there a way to pass auto as an argument in C++?

c++,function,arguments,auto
Is there a way to pass auto as an argument to another function? int function(auto data) { //DOES something } ...

Segmentation fault in expression template using type created with auto keyword

c++,c++11,segmentation-fault,auto,expression-templates
I am constructing code with expression templates for computational kernels. My question is very short: Why does GNU G++ give a segfault (4.9.1, compiled with -O3) on the line containing the += in the following example: // Like this it crashes auto expression = Ix_h( Ix(u) ); ut += expression;...

C++11 auto variable : float array vs auto

c++11,auto
Size of float array declared is suing auto is different than actual size. why is it so ?? For e.g: declaration: float duto[] = {2.2222f,2.223f,34.5f,1.0f,9.0f}; auto dutot = {2.2222f,2.223f,34.5f,1.0f,9.0f}; Size print: std::cout << " float array size v: " << sizeof(duto)<<std::endl; std::cout << " auto v: " << sizeof(dutot)<<std::endl; Output:...

using auto/pointer to child, access child's parent method

c++,c++11,parent-child,auto
I'm trying to figure out how to access a parent method using a pointer to the child class. Any ideas? I looked for examples but found things that were more different than not. child access parent - different. I have a test class with the following: auto canvasObj = parseCanvas(one,...

how to dissable and enable auto rotate on swift?

ios,swift,rotation,auto
On general settings I allow portrait and landscapeleft , landscaperight modes. I want to turn off landscape modes. On viewController I write this code: override func shouldAutorotate() -> Bool { return false } However, the auto rotation works ignoring this function. How to disssable and enable autorotation on swift? IOS...

How to disable phonegap autoreload (At least while dev)

api,cordova,reload,auto
here is my issue : I've installed phonegap and phonegap desktop. http://code.riffzone.net/phonegap-install-msw/ Now, when I'm working on my application (Jquery Mobile), I'm testing it on a browser, but I've ALwAYS the page reloading again and again every 2 secondes, I can't even write something in the form input, reloading to...

Why auto is deduced to int instead of uint16_t

c++,c++11,auto,integer-promotion
I have the following code: uint16_t getLastMarker(const std::string &number); ... const auto msgMarker = getLastMarker(msg.number) + static_cast<uint16_t>(1); static_assert(std::is_same<decltype(msgMarker), const int>::value, "Should fail"); static_assert(std::is_same<decltype(msgMarker), const uint16_t>::value, "Should not fail"); and I expect that the first assertion will fail and second one will not. However gcc 4.9.2 and clang 3.6 do the...

Member function, unable to deduce ‘auto’

c++,function-pointers,auto
I'm wondering if it's possible in c++ to get pure function pointer from member function of an object. class AS { int x; public: AS(int xx) { x = xx; } void ww(void* p) { std::cerr << "AS::ww " << x << std::endl; } }; void exp() { void* pp...

auto type deduction not working as expected

c++,c++11,visual-c++,auto
this is much like this question Why must a short be converted to an int before arithmetic operations in C and C++? however there is a sub-question why the compiler diagnoses a warning in one case and an error in the other case ,with the exact same expression. I really...

what's that type deduced by auto?

c++,auto
In some rare situation, i will want to explicitly declare variable? so what's the type of f4 ? code below come from cppreference #include <random> #include <iostream> #include <memory> #include <functional> struct Foo { int data = 10; }; int main() { using namespace std::placeholders; // for _1, _2, _3......

Adding an Auto Delete for files to the end of a Script File

bash,terminal,auto
So I have a script that takes the zip files in the 2 folders and unzips them into new folders which all works and runs fine (Folder Names Changed) But I want to first Know if it is possible to after the files have been unziped and placed in there...

In whats case should use auto type?

c++,lambda,auto,c++14,std-function
I have some questions regarding when use auto Explicit, it is not more clear despite the context? Cases that are better be explicit instead of auto? And with lambda functions use auto how these auto f = [](auto v, auto x) {}; //or std::function<void(auto v, auto x)> f; Thanks......

Bash script - Auto fill answer

linux,bash,shell,sh,auto
I have a bash script that has several questions, is it possible to automatically fill the answers ? ./script.sh install answers in order y 2 1 n n How can I do that in bash ? edit: is it possible to only pass the first answer ? echo "y" |...

How to use lambda auto parameters in C++11

c++,c++11,auto
I have a code in C++14. However, when I used it in C++11, it has an error at const auto. How to use it in C++11? vector<vector <int> > P; std::vector<double> f; vector< pair<double, vector<int> > > X; for (int i=0;i<N;i++) X.push_back(make_pair(f[i],P[i])); ////Sorting fitness descending order stable_sort(X.rbegin(), X.rend()); std::stable_sort(X.rbegin(), X.rend(),...

iterating over unordered_map

c++,c++11,auto,unordered-map
In a for loop with auto, an iterator iterates over an unordered_map. Like this: using RuleIndex = std::unordered_map<uint, Symbol*>; RuleIndex rule_index; for(const auto & rule_pair : rule_index ) { std::cout << rule_pair.first << ": "; printList(rule_pair.second, 0); std::cout << std::endl; } Assume all variables are defined properly, since the code...

Accessing deduced method calls with auto keyword from header file

c++,c++14,auto
Take the following class: BasicOperations.h template<typename T1> class BasicOperations { private: T1 num; public: template<typename T1, typename T2> BasicOperations() : num(0) {} template<typename T1, typename T2> auto addition(T1 num1, T2 num2) -> decltype(T1 + T2) const { return num1 + num2; } } I have begun using auto and from...

Sublime Text 2 Auto complete stopped working randomly

text,auto
I was writing some HTML in Sublime Text 2 and I noticed that the auto complete feature was no longer working. Just so we're clear, I'm not referring to the dropdown menu that suggests elements but rather the tag completion. Normally what happens is if I were to type an...

Autocreate nested foreach statement

c#,selenium-webdriver,foreach,iterator,auto
I have a simple scenario I am trying to workout which involves auto creating nested foreach statements. What the method is to do is take the int value passed into the method and based on this, it should automatically create nested foreach statements. public static String[] ValuesAdd1 = { "a",...

destructor called before temporary should be out of scope

c++,auto,c++14,decltype,visual-studio-2015
I have a bit of code which fails under VS2015, but works under GCC. I'm pretty sure the bug is with Visual Studio but want to be sure that my understanding of decltype(auto) is correct. #include <iostream> using namespace std; string zero_params() { return "zero_params called."; } template< typename F...

I need help quick! and whould be so greatful if anyone can help me solve this puzzle.

html,css,auto,adjustment
Ok am making a webpage that auto adjust so i can use it for any device. and ive fixed alsmost everything so it size adjust propperly except my catagroys, its the .hip div. they only line underneith eachother when the web size is small. but i want the acctualy Buttons/images...

Returning a const pointer to a const data member and the 'auto' keyword. A bit confused

c++,pointers,c++11,const,auto
I've recently been learning C++ and just today have been introduced to const and the concept of const correctness. In an attempt to better understand the theory, I've been writing a series of simple programs to make sure that I understand the concept correctly. I thought I understood everything, but...

C++14 auto deduction error: function returns an array

c++,arrays,auto,c++14
community! I'm trying to apply new C++14 features and unexpectedly encountered on error while I was attempting to pass the const char[] argument to the function given below: decltype(auto) autofunc( const auto& a) { cout<<"Hello World\n"; cout<<a<<endl; } auto lambd = [](const auto& word){ autofunc(std::forward< decltype(word) > (word));}; int main()...

PHP + SQLi - Auto incrementing ID's

php,mysql,mysqli,increment,auto
I've come across a problem where in my PHP, for some reason I have to define what the ID is or I get my "die" message, even though the table structure is set to auto increment, here is my PHP: $query = mysqli_query($link, "INSERT INTO users (username, email, passhash, reg_ip,...

C++ constexpr auto member function. Clang issue?

c++,c++11,auto,c++14,constexpr
#include <utility> struct A { constexpr auto one(int a) { return std::integral_constant<int, _data[a]>{}; } constexpr int two(int a) const { return _data[a]; } int _data[10]; }; int main() { constexpr auto ex = A{{1,2,3,4,5,6,7,8,9,10}}; std::integral_constant<int, ex.two(3)> b{}; } The code above will not compile in trunk Clang. The error...

Status bar notification on incoming SMS in Android?

android,notifications,sms,statusbar,auto
I am a beginner in Android programming. I am trying to implement a notification for incoming SMS in android device. However, I am able to create a toast for the same but unable to implement a status bar notification. Can nyone help me ? For now I have coded this...

Segfaults when declare/define a function returning a valarray with auto return type

c++,auto,c++14
Can somebody please help me to understand why the following code segfaults? The code works if I declare/define mk to return std::valarray<int>. I guess I am not quite sure what auto does here. #include <iostream> #include <valarray> auto mk(int x) { return x * std::valarray<int>{1}; } int main() { auto...

C++1z handling of a == test with std::initializer_list with and w/o const in an auto function

c++,auto,c++14,initializer-list,c++1z
c_NEXT is defined in class AskBase: struct Status { static auto constexpr c_ROOT {-999}; static auto constexpr c_PREVIOUS {-1}; static auto constexpr c_NEXT {1}; static auto constexpr c_EOF {999}; }; Later c_NEXT is used in template<typename Container> class AskUI auto run(){ auto status = AskBase::Status::c_NEXT; for (typename Container::iterator ii=m_asks.begin(); ii!=m_asks.end();...

Auto and copy elision

c++,c++11,auto,copy-elision
What exactly are the rules for copy elision when one is using auto and commits to a specific type ? (see: GotW - Almost always auto). From my understanding, the move/copy constructor is required to be accessible even though it's not generally used. But then what's the difference between unique_ptr...

Difference between auto&& and auto

c++,c++11,auto,rvalue-reference
So, I've made such a class, and launched main: class my_class{ public: my_class(): status("constructor"){ cout << "constructor" << endl; } my_class(my_class&& obj): status("move constructor"){ cout << "move constructor" << endl; } my_class(const my_class& obj): status("copy constructor"){ cout << "copy constructor" << endl; } my_class& operator=(my_class&& obj){ cout << "move assignment"...