C++ template function deducing return type

I’m trying to create a simple template function which takes a callable object and a couple of int values, and returns the result of invoking the callable with those values. How do I get the function to figure out the return value’s type, and also be able to create such a type with its default… Read More C++ template function deducing return type

How does std::unordered_map find values?

When iterating over an unordered map of values, std::unordered_map<Foo, Bar>, it will use an iterator pointing to values instd::pair<Foo, Bar>. This makes it seem like std::unordered_map stores its values internally as std::pairs of values. If I’m not mistaken, std::unordered_map works by hashing the key and using that for lookups. So, if the internal structure is… Read More How does std::unordered_map find values?

Rust taking input in the same variable more than once behavior. Can anyone explain?

So I tried to take input into the same variable called input with a loop. It behaves in a way that I do not know why. Here is the snippet use std::io::stdin; fn main(){ println!("Please Give Input: "); let mut input = String::new(); loop{ stdin().read_line(&mut input).expect("Error While Input"); print!("{input}"); } } on the first input… Read More Rust taking input in the same variable more than once behavior. Can anyone explain?

how to copy a map <string, int> into a vector <int, string>

my code copies the map in the same order map <string, int> to vector <string, int> I want this instead map <string, int> to vector <int, string> is it possible with std copy? #include <iostream> #include <vector> #include <map> #include <iterator> #include <fstream> using namespace std; int main(){ fstream fs; fs.open("test_text.txt"); if(!fs.is_open()){ cout << "could… Read More how to copy a map <string, int> into a vector <int, string>

How to guarantee that std::cout always uses n width

I have output that looks like this: BTC-USDT [FTX] 20460.91 20470.09 BTC-USDT [BINANCE_US] 20457.34 20467.28 BTC-USDT [BINANCE_US] 20457.50 20467.28 I would like it to look like this: BTC-USDT [ FTX] 20460.91 20470.09 BTC-USDT [BINANCE_US] 20457.34 20467.28 BTC-USDT [BINANCE_US] 20457.50 20467.28 I think I am close with this code, but I am confused by setw() std::cout… Read More How to guarantee that std::cout always uses n width

How can std::reference_wrapper<int> use opretor += if std::reference_wrapper doesn't have operator+=

[Solved] Thank you all, I didn’t even know about user-defined conversion function and how it works. Why it’s possible to use std::reference_wrapper::operator+=, if such operator does not exist, are there some implicit conversions? #include <iostream> #include <functional> #include <boost/type_index.hpp> using boost::typeindex::type_id_with_cvr; template <typename C> void test(C c) { c += 1; } int main() {… Read More How can std::reference_wrapper<int> use opretor += if std::reference_wrapper doesn't have operator+=

How does std::is_polymorphic identify polymorphism?

I tried to understand the working of std::is_polymorphc in C++. This is defined in type_traits.h: template <class _Ty> struct is_polymorphic : bool_constant<__is_polymorphic(_Ty)> {}; // determine whether _Ty is a polymorphic type template <class _Ty> _INLINE_VAR constexpr bool is_polymorphic_v = __is_polymorphic(_Ty); I am not able to find the source code for __is_polymorphic. Could someone help me… Read More How does std::is_polymorphic identify polymorphism?