How to infer return type of function returned from a function in c++

Consider this code template <typename OUT> auto fun(const int& x) { return [&](function<OUT(int)> fout) -> decltype(fout(x)) {return fout(x);}; } calling function auto w = fun(23)([](const auto& x) {return x*2;}); The function fun when called cannot deduce the return type as above code fails compilation with an error saying that "candidate template ignored: couldn’t infer template… Read More How to infer return type of function returned from a function in c++

How to access and convert pair value?

vector<pair<int,char>>alpha; for(int i=0;i<26;i++){ if(letter[i] > 0) { alpha.push_back(pair<int,char>(letter[i],(i+’A’))); } } sort(alpha.begin(),alpha.end()); for(auto& val:alpha){ string str = val.second; } I was trying to convert map value (which was char type) into string type using auto. I need to push those char into string. How could I solve this? >Solution : You could do string str; for(auto&… Read More How to access and convert pair value?

Why does this need auto?

auto data = new char[480][640][3](); char data = new char[480][640][3](); First works. Second doesnt. Why? Isn’t auto supposed to just replace itself with the type of the initializer? >Solution : Because the type isn’t char. The type is char(*)[640][3] and the declaration would be written as char (*data)[640][3] = new char[480][640][3]();

How to generate a random number with the amount of digits, the user enters?

I’m making a small math program for practice purposes. The user is supposed to enter, how many digits the two summands should have. I did this as shown below: import random digits = int(input(“How many digits should the numbers have? “)) if digits == 1: while True: num1 = random.randint(0,9) num2 = random.randint(0,9) solution =… Read More How to generate a random number with the amount of digits, the user enters?