Posts

Showing posts with the label auto

Difference between const auto & and auto & if object of reference is const

// case 1 const int i = 42; const auto &k = i; // case 2 const int i = 42; auto &k = i; Do we need the const keyword before auto in this scenario? After all, a reference (k) to an auto-deduced type will include the top level const of the object (const int i). So I believe k will be a reference to an integer that is constant (const int &k) in both cases. If that is true, does that mean that const auto &k = i; in case 1 is replaced by the compiler as just const int &k = i; (auto being replaced with int)? Whereas in case 2, auto is replaced with const int? auto keyword automatically decides the type of the variable at compile time. In your first case, auto is reduced to int where it's reduced to const int in the second case. So, both of your cases are reduced to the same code as: const int &k = i; However, it's better to have the const explicitly for better readability and to make sure your variable TRULY is const. Type deduction with auto works l...

Why does const auto &p{nullptr} work while auto *p{nullptr} doesn't in C++17?

This definition works: const auto &b{nullptr}; while this fails: auto *b{nullptr}; I have tried to compile this in Visual C++, GCC, and Clang. They all complain "cannot deduce type". In the second case, shouldn't b be deduced to have some type like std::nullptr_t? It's because you declare b to be a pointer, and initialize it to be a null pointer. But a null pointer to what type of data you don't say, so the compiler can't deduce the type. If you want b to be a std::nullptr_t object, you should drop the asterisk: auto b{nullptr}; decltype(nullptr) is std::nullptr_t. so with const auto &b{nullptr}; // auto is std::nullptr_t // b is a reference to a temporary (with lifetime extension) but nullptr is NOT a pointer (even if it is convertible to). so auto *b{nullptr}; is invalid. You might use instead auto b{nullptr}; // auto is std::nullptr_t nullptr is of type std::nullptr_t. As a nullptr does not point to anything, there is no corresponding...

Lambda returning itself: is this legal?

Consider this fairly useless program: #include <iostream> int main(int argc, char* argv[]) { int a = 5; auto it = [&](auto self) { return [&](auto b) { std::cout << (a + b) << std::endl; return self(self); }; }; it(it)(4)(6)(42)(77)(999); } Basically we are trying to make a lambda that returns itself. MSVC compiles the program, and it runs gcc compiles the program, and it segfaults clang rejects the program with a message: error: function 'operator()<(lambda at lam.cpp:6:13)>' with deduced return type cannot be used before it is defined Which compiler is right? Is there a static constraint violation, UB, or neither? Update this slight modification is accepted by clang: auto it = [&](auto& self, auto b) { std::cout << (a + b) << std::endl; return [&](auto p) { return self(self,p); }; }; it(it,4)(6)(42)(77)(999); Update 2: I understand how to write a functor that returns itself, or ho...