Tag: c#
-
How can a big number fit precisely into `double` with 32-bit GCC?
6 Consider the following code: #include <iostream> int main() { long long x = 123456789123456789; std::cout << std::fixed; auto y = static_cast<double>(x); // (1) std::cout << static_cast<long long>(y) << "n"; // (2) std::cout << y << "n"; std::cout << (x == static_cast<long long>(y)) << "n"; // (3) std::cout << static_cast<long long>(static_cast<double>(x)) << "n"; // (4)…
-
How to reuse the return type of a function call inside a requires expression?
7 I’m writing a concept that checks if a type can be used in an expression that composes 2 functions: template<typename T> concept C = requires(T t) { f(g(t)); }; i.e., I want to check if for a given object t of type T, I can call g(t) and then use the resulting value as…
-
Why is a stray semicolon no longer detected by `-pedantic` modern compilers?
6 The following snippet generates compilation errors on adding -pedantic and -Werror on compilers that are a bit old. #include <cstdint> #include <iostream> int add(int a, int b){ return a + b; }; // <– stray semicolon int main (){ return 0; } However this does not happen newer compiler versions. Please find a matrix…
-
GraphQL HotChocolate 13: Adding PaginationAmountType to be backwards compatible for old clients
0 I’m migrating a quite old GraphQL server (<10) to the latest version (13). It looks everything is working OK after doing the necessary changes. The only thing is that HotChocolate dropped the PaginationAmountType class. The queries done by clients of my service do not seem to be compatible anymore. As this is a critical…
-
std::ranges::find vs std::find
8 Consider this code: #include <vector> #include <iostream> #include <cstdint> #include <ranges> int main() { struct S { int a; int b; bool operator==(int other) const { return a == other; } }; std::vector<S> iv{ {1, 2}, {3, 4} }; // this works if (auto const it{std::find(iv.begin(), iv.end(), 1)}; it != iv.end()) { std::cout <<…
-
Why do I need to specify the type of a default constructed object in this situation?
9 I don’t understand why in foobar below I need to specify std::vector<int>{} whereas in foobar2 I do not: #include <iostream> #include <memory> #include <vector> #include <tuple> std::tuple<std::unique_ptr<int>, std::vector<int>> foobar() { std::unique_ptr<int> test = std::make_unique<int>(42); return { std::move(test), {} }; // <= this is a syntax error // return { std::move(test), std::vector<int>{} } // <=…
-
Why does ranges::for_each return the function?
10 The legacy std::for_each returns function as the standard only requires Function to meet Cpp17MoveConstructible according to [alg.foreach]: template<class InputIterator, class Function> constexpr Function for_each(InputIterator first, InputIterator last, Function f); Preconditions: Function meets the Cpp17MoveConstructible requirements. [Note: Function need not meet the requirements of Cpp17CopyConstructible. end note] This is reasonable since the user may want to…
-
Is it ok to use std::ignore in order to discard a return value of a function to avoid any related compiler warnings?
10 I know that you can use static_cast<void>, but it just seems too verbose for me, and not reflecting the original intent that I want to discard a return value, not to cast it to anything. Recently I stumbled upon std::ignore, which can accept a value of any type, the name is clear and readable,…
-
How to avoid manual inlining inside LINQ query to use as an expression and convert to SQL
0 I am trying to do some projections in LINQ to convert to DTOs. The goal is to do it with expressions so that it is translated directly to SQL. It works perfectly if I do everything inlined like this : return workHeader.Select(x => new WorkHeaderDto { Id = x.Id, StartDate = x.Works.Min(w => w.StartDate),…
-
Is it undefined behaviour to use pointer after allocated memory?
10 I have the following code: uint8_t buffer[16]; uint8_t data[16]; uint8_t buffer_length = 16; uint8_t data_length = 0; memcpy(buffer + buffer_length, data, data_length); memcpy should be a no-op, because data_length is zero. However buffer + buffer_length points just outside of the allocated memory. I wonder if it could trigger some kind of undefined behaviour? Should…