« first day (1485 days earlier)      last day (1359 days later) » 

6:30 AM
S::S(std::string str) try : data(std::stoi(str)) {
    std::cout << "ctor2 called, data = " << data << '\n';
} catch(const std::exception&) {
    std::cout << "ctor2 failed, string was '" << str << "'\n";
    throw; // ctor's catch clause should always rethrow
}
Can anyone explain this syntax?
 
well there's

int func() try{}catch(...){}

and this is that version for constructors with an initializer list
 
Hey, that I didn't know about... Is this syntax only implementable for ctors and dtors?
 
no, any function
 
Like for example? I am not able to map this to the following kind of function:

int foo(int m) {
    try {
        m++;
    }
    catch (const std::exception& e) {
        throw;
    }
}
 
int foo(int m)
try {
m++;
}
catch (const std::exception& e) {
throw;
}
just leave out the surrounding braces, but you still need to return something if you have a return type
 
6:38 AM
Oh! gotcha. Thanks :)
Also, is there any difference between the following 2 ctors?:
ctor() { }
ctor() = default;
 
yeah on of them is trivially default constructible the other isn't
 
How so? I really have never come across this😅
Also, which is better?
 
ctor() = default; //this one's better
 
Umm... cppreference does not list this. Where do I learn about this?
 
@d4rk4ng31 https://en.cppreference.com/w/cpp/language/default_constructor
Trivial default constructor
The default constructor for class T is trivial (i.e. performs no action) if all of the following is true:

The constructor is not user-provided (i.e., is implicitly-defined or defaulted on its first declaration)
 
6:45 AM
Ah! got it
@PeterT Oh! nice. Thanks :)
Oh! So, it comes in handy when you've declared some non-default ctor, and wanna use default for the rest ctors, as the compiler does not auto-generate them in that case... Wow! that's quite good :)
 
 
1 hour later…
8:06 AM
istream& operator >> (istream& in, derived& d)
{
    in >> static_cast<base&>(d);
    return in;
}
In the code, static_cast<base&>(d) is an rvalue, right?
 
8:38 AM
@d4rk4ng31 nah that's an lvalue
look at en.cppreference.com/w/cpp/language/value_category in the examples for lvalues a cast expression to lvalue reference type, such as static_cast<int&>(x);
would be pretty weird if using a static cast could trigger a move-construction or move-assignment
 
 
3 hours later…
12:06 PM
Would a sorting algorithm that randomly compares two indexes in an array until it is sorted be efficient?
Efficient not as per most optimized but good enough
Asking because data is accessed randomly on RAMs (Random Access Memory), right?
 
nwp
Comparing elements randomly doesn't sort an array. You'd have to properly define the algorithm and then check.
You can access data sequentially in RAM just fine. It'll even tend to be faster because of prefetchers.
 
Just thought of what the algorithm would be like, and shuddered..
 
nwp
Read through bogosort to lose all hope and try again.
 
Lmao, @nwp
Was literally there a few seconds ago 🤣
 
12:49 PM
hi
Can I use unordered map with custom pointer as hash key
my idea is that pointer is just a long value so maybe do we need to cast it to long first then use it as key?
Oh I just saw an answer about the same on stackoverflow _:)
13
Q: Hashing pointers as Keys for unordered_map in C++ STL

Ammar HusainI posted a similar quetion regarding using pointers as Keys on maps in C++ STL. How are pointers hashed in unordered_maps when used as Keys. More specifically if I define: std::unordered_map< CustomClass*, int > foo; Would the default C++ std::hash implementation work to handle these pointers?...

 
nwp
Instead of casting you are supposed to add std::hash<YourCustomPointer> and define how to hash it which can use the internal pointer and defer to std::hash<T*> if that does what you want.
 

« first day (1485 days earlier)      last day (1359 days later) »