Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: To C or not to C++ Date: Sat, 13 Aug 2022 13:12:30 -0700 Organization: None to speak of Lines: 31 Message-ID: <87a687ewcx.fsf@nosuchdomain.example.com> References: <2868500762@darkrealms.ca> <30afe66c-8f37-488e-bc5a-ad9d3aa211f1n@googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: reader01.eternal-september.org; posting-host="5b4f929b518576a6dd9016cb3a20a05c"; logging-data="3061792"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18dKjzEhiB4gDPXyDXujAMk" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:91JUTwzHA9v8iUUzA+bNGyxe3z8= sha1:ASaQG4vfCBJmbiHhXZrMqgBz1+A= Xref: csiph.com comp.lang.c++:85915 Malcolm McLean writes: > On Saturday, 13 August 2022 at 20:13:33 UTC+1, David Brown wrote: [...] >> There is an old-fashioned style of C programming where you use "#define >> NO_OF_THINGS 20" and use function-like macros for code size/speed >> efficiency. But in modern C you'd use "static const int no_of_things = >> 20;", or "enum { no_of_things = 20 };" for the purpose, and static >> inline functions instead of function-like macros. >> > Generally you'd do that to declare an array rather than use dynamic allocation. > But on most systems the speed improvement no longer justifies the > extra complexity and scope for error you introduce by having a hard limit. In C, `static const int no_of_things = 20;` doesn't make `no_of_things` a constant expression. You can use it as an array length at block scope *if* the implementation supports variable-length arrays (VLAs), which were made optional in C11. This: static const int no_of_things = 20; int arr[no_of_things]; is illegal (a constraint violation) in C at file scope. (It works in C++, but I'd use constexpr rather than const.) enum { no_of_things = 20; } does give you a constant expression, but is limited to type int. (C23 will change that.) -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */