Tag: c#
-
Why gcc is so much worse at std::vector vectorization of a conditional multiply than clang?
25 Consider following float loop, compiled using -O3 -mavx2 -mfma for (auto i = 0; i < a.size(); ++i) { a[i] = (b[i] > c[i]) ? (b[i] * c[i]) : 0; } Clang done perfect job at vectorizing it. It uses 256-bit ymm registers and understands the difference between vblendps/vandps for the best performance possible.…
-
declval()() – what does this mean in the below context?
18 This is from: https://github.com/gcc-mirror/gcc/blob/master/libstdc++-v3/include/std/type_traits template<typename _Xp, typename _Yp> using __cond_res = decltype(false ? declval<_Xp(&)()>()() : declval<_Yp(&)()>()()); … template<typename _Tp1, typename _Tp2> struct __common_reference_impl<_Tp1, _Tp2, 3, void_t<__cond_res<_Tp1, _Tp2>>> { using type = __cond_res<_Tp1, _Tp2>; }; I’m trying to figure out what _Xp(&)() is – is it a function call signature? i.e. a constructor? Doesn’t really…
-
Why is initializing a string to “” more efficient than the default constructor?
11 Generally, the default constructor should be the fastest way of making an empty container. That’s why I was surprised to see that it’s worse than initializing to an empty string literal: #include <string> std::string make_default() { return {}; } std::string make_empty() { return ""; } This compiles to: (clang 16, libc++) make_default(): mov rax,…
-
Faster algorithm for max(ctz(x), ctz(y))?
13 For min(ctz(x), ctz(y)), we can use ctz(x | y) to gain better performance. But what about max(ctz(x), ctz(y))? ctz represents "count trailing zeros". C++ Version (Compiler Explorer) #include <algorithm> #include <bit> #include <cstdint> int32_t test2(uint64_t x, uint64_t y) { return std::max(std::countr_zero(x), std::countr_zero(y)); } Rust Version (Compiler Explorer) pub fn test2(x: u64, y: u64) ->…
-
Why in C# an explicit decimal => long conversion operator is called implicitly, losing precision?
6 The following C# program silently and implicitly calls an explicit decimal-to-long conversion operator, losing precision. I don’t understand why this happens. As far as I understand, in C# explicit operator should not be called implicitly by the language. Especially that in this case the silent explicit conversion is losing precision (1.1M => 1L). This…
-
In C++ Inheritance, Derived class destructor not called when pointer object to base class is pointed to array of derived class
17 I have an Animal class with constructor and destructor. Cat have a private Brain* attribute. Upon construction, Cat creates his Brain using new Brain(); Upon destruction, Cat deletes his Brain. I don’t understand why The cat’s and brain’s destructors not called, when my Base class destructor is virtual? #include <iostream> using std::cout ; using…
-
Is every null pointer constant a null pointer?
9 From the C17 draft (6.3.2.3 ¶3): An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.67) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a…
-
How to combine `Select` and `Where`
7 I am doing something very simple and straightforward, and I cannot help but suspect / wish that it could be made a bit shorter. For the sake of the example, let us suppose that I have an IEnumerable<int?> and all I want is an IEnumerable<int> yielding only the non-null values. Here is some code…
-
What is the best approach for using std::ranges/std::views with std::expected in C++23?
8 Let me describe a scenario, first we have a function which returns us some sort of data which we can’t be sure about the validity of, i.e. auto random_predicate() -> bool { int v = uniform_dist(e1); // uniform distribution 1 to 100 return (v % 5); } where uniform_dist() is an appropriately seeded uniform…
-
Deleted move constructor in base class does not stop derived class object to be returned from a function
12 Given base class A and derived class B, A has deleted move constructor: class A { public: A() {} A(const A&) = default; A(A&&) = delete; }; class B : public A { }; In such case, the following function does not compile because of deleted move constructor: A f() { A a; return…