Tag: c#
-
Is gcc wrong to allow the initialization of a const array member with another array reference?
8 While (re)implementing a simple constexpr map, I wrote this (godbolt): template <class key_type, class value_type, int N> class flat_map { private: struct pair { key_type key; value_type value; }; const pair elements[N]; public: consteval flat_map(const pair (&arr)[N]) noexcept : elements(arr) // works on gcc?! {} [[nodiscard]] consteval value_type operator[](const key_type key) const { for…
-
Why can a class that does not implement member functions be instantiated in C++?
14 For example: class B; class A { public: B f(); }; int main(int, char**) { A a; // no exception and error return 0; } The class A may be an incomplete type. Why can class A be instantiated in this example? I know that the program will fail to compile when the member…
-
No compiler warning for ‘const’ pointers of non POD types when overriding a method
7 An example where one is overriding POD return type without const in the derived class: struct B1 { virtual const int* f(); }; struct D1 : B1 { int* f() override; }; Compilers like Clang and GCC raise a warning: invalid covariant return type for ‘virtual int* D1::f()’ When same scenario is applied but…
-
std::move and lifetime of temporary objects
11 Can someone explain the execution order of this code? struct Foo { ~Foo() { std::cout << "1"; } }; int main() { const Foo& bar = Foo(); const Foo& baz = std::move(Foo()); std::cout << "2"; } The following code prints 121. I understand why I get 1 after 2, it’s because the lifetime of…