- enum class or enum? - 9 Updates
- question for NRVO in if-branch - 1 Update
- Users needed - 4 Updates
- who's at fault, me or compiler? - 3 Updates
- Should this compile? - 1 Update
Juha Nieminen <nospam@thanks.invalid>: Jul 17 05:56AM >> cast). Sometimes being able to index an array with enumerated names can be >> handy (when those names refer to elements of an array). > ok, this I also have been thinking. enum class makes it a bit difficult Not difficult, just a bit annoying that you need to explicitly cast the name, eg. like value = table[unsigned(enumeratedName)]; |
JiiPee <no@notvalid.com>: Jul 17 11:34AM +0100 On 17/07/2020 06:56, Juha Nieminen wrote: > Not difficult, just a bit annoying that you need to explicitly cast the name, > eg. like > value = table[unsigned(enumeratedName)]; value = table[North]; vs value = table[unsigned(Direction::North)]; If that is done alot in the code the second one is not so nice. But Direction curdir = Direction::North; is fine,... its just that as an index it gives troubles. |
Thiago Adams <thiago.adams@gmail.com>: Jul 17 11:19AM -0700 On Wednesday, July 15, 2020 at 1:56:26 PM UTC-3, Paavo Helde wrote: > Depens on the project scope. If this is a hundred line program developed > during a weekend, then plain enum is fine. If this is a million-line > program developed over decades, I would advocate for enum class. If you write a library and publish it you will not know where this library will be used. In this scenario extra care is taken to avoid name conflicts. This is not only for enuns but any variable. Namespaces can be used in C++ and suffixes in C. In my opinion beautiful code is when you look at the source code and everything looks to be created in one shot by the same person. This is not always possible to have. Different programmers, different libraries and styles can live inside the same source code. My advice: If you are writing the code for yourself or your company, (not for publishing a library) then keep everything small. The primarily characteristic you need is "easy to change" and not "protect for the unknown". |
Paavo Helde <eesnimi@osa.pri.ee>: Jul 17 10:27PM +0300 17.07.2020 21:19 Thiago Adams kirjutas: > (not for publishing a library) then keep everything small. > The primarily characteristic you need is "easy to change" and > not "protect for the unknown". If you need to change something related to enums, the hard part is not the length of names to be typed. You know, there are things like autocompletion menus, copy-paste, find-and-replace, etc. The hard part is to ensure that after the changes the program still works correctly. For example, you might change a function parameter from enum to bool. Guess what? A call with plain enum value still compiles and most probably does not do the right thing. Enum class will fail to compile, this is a major win in the "easy to change" compartment. |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jul 17 08:43PM +0100 On 15/07/2020 17:18, JiiPee wrote: > Quick question. Sometimes I think whether to use enum class or enum. enum is shorter, so is it sometimes better? > Example a tic tac toe game where board piece is enum: > enum class Piece {Empty, X, O}; I would think long and hard about making "Empty" a "Piece". /Flibble -- "Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin "You won't burn in hell. But be nice anyway." – Ricky Gervais "I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais "Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?" "I'd say, bone cancer in children? What's that about?" Fry replied. "How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil." "Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say." |
Keith Thompson <Keith.S.Thompson+u@gmail.com>: Jul 17 01:02PM -0700 > compiles and most probably does not do the right thing. Enum class > will fail to compile, this is a major win in the "easy to change" > compartment. If you're using it as an index, you can write an overloaded operator[] that accepts an operand of your enum class type and *not* of any integral type. You'd have to create your own array-like class rather than using raw arrays or std::vector. Like many things, it's a tradeoff between convenience and safety. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips Healthcare void Void(void) { Void(); } /* The recursive call of the void */ |
Thiago Adams <thiago.adams@gmail.com>: Jul 17 01:52PM -0700 On Friday, July 17, 2020 at 4:27:50 PM UTC-3, Paavo Helde wrote: ... > enum to bool. Guess what? A call with plain enum value still compiles > and most probably does not do the right thing. Enum class will fail to > compile, this is a major win in the "easy to change" compartment. I will tell what I do in this case. If I need to change something that changes the function contract I rename that function and compile again. Doing this ensures that I will review all callers. When I am satisfied with the review I put the original name back. The approach is more generic than only prevent wrong conversion between enum and bool. |
Thiago Adams <thiago.adams@gmail.com>: Jul 17 02:12PM -0700 On Wednesday, July 15, 2020 at 1:18:43 PM UTC-3, JiiPee wrote: > enum is shorter, so is it sometimes better? > Example a tic tac toe game where board piece is enum: > enum class Piece {Empty, X, O}; [...] Using 'Empty' here will probably cause a conflict. enum A {Empty, A1, A2}; enum B {Empty, B1, B2}; Once I have suggested at comp lang c a feature for C that gives bigger char literals. Today we can use like this: int c = 'abcd'; //up to four chars (int) and this will create a unique value. How we could add more? My suggestion was to create a hash that give the same result as it is today until 4 chars. After that we hash differently but universally (all compilers would do the same) What is the relation with enums? We could have a : enum Color { 'blue', 'red', 'yellow' }; And we could reuse 'red' enum Alarm { 'red' 'yellow' }; enum Alarm a = 'red'; a = 'blue'; //error blue is not a valid alarm value enum Color c = 'red'; In case 'red' and 'yellow' give us the same hash then this would be a compiler error at color enum. This is a property similar of we had for nullptr. nullptr is a constant that can be applied for any pointer type. struct X* pX = nullptr; struct Y* pY = nullptr; for those enuns types 'red' for instance can be applied for any enum that have 'red' in their list. So, here if we want to use the world 'empty ' this could be used in any enum according with the context instead of E1::Empty and E2::Empty; This is also a reson for people use int or #defines instead of enums. int makes the 'set' of values open. For instance; #define ERROR1 1 #define ERROR2 2 #define ERROR3 3 int F1(); //returns ERROR1 or ERROR2 int F2(); //returns ERROR3 or ERROR2 using enuns to represent each set of errors we cannot have the same values shared. |
Keith Thompson <Keith.S.Thompson+u@gmail.com>: Jul 17 03:54PM -0700 > Using 'Empty' here will probably cause a conflict. > enum A {Empty, A1, A2}; > enum B {Empty, B1, B2}; Not if you use "enum class". > Today we can use like this: > int c = 'abcd'; //up to four chars (int) > and this will create a unique value. There's no guarantee that the value is unique (or that int is wider than 16 bits). I suppose you *could* modify the language to provide more guarantees for multi-character constants, but my own inclination is to avoid them, and possibly deprecate or remove them in a future standard. [...] -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips Healthcare void Void(void) { Void(); } /* The recursive call of the void */ |
anhonghe@gmail.com: Jul 17 02:47PM -0700 Hi Experts, I have a question for following code: string p(bool x) { if(x){ const string s = "1111111111111111111111111111111111111111111111111"; std::cout << "var address: " << &s << std::endl; std::cout << "buf address: " << (void *)(s.data()) << std::endl; return s; // expect NRVO but actually copy ctor? } else{ const string s = "2222222222222222222222222222222222222222222222222"; std::cout << "var address: " << &s << std::endl; std::cout << "buf address: " << (void *)(s.data()) << std::endl; return s; // expect NRVO but actually copy ctor? } } int main() { { std::cout << "call p(true), expect NRVO" << std::endl; auto s = p(true); std::cout << "var address: " << &s << std::endl; std::cout << "buf address: " << (void *)(s.data()) << std::endl; } std::cout << "===============" << std::endl; { std::cout << "call p(false), expect NRVO" << std::endl; auto s = p(false); std::cout << "var address: " << &s << std::endl; std::cout << "buf address: " << (void *)(s.data()) << std::endl; } return 0; } $ g++ main.cpp -std=c++17 output is: call p(true), expect NRVO var address: 0x7fffc084bd50 buf address: 0x7fffb9b97280 var address: 0x7fffc084bda0 buf address: 0x7fffb9b972c0 =============== call p(false), expect NRVO var address: 0x7fffc084bd50 buf address: 0x7fffb9b972c0 var address: 0x7fffc084bda0 buf address: 0x7fffb9b97280 as you see, I expect it uses NRVO because when reach the string constructor, the compiler already sure the string it's constructing will get returned. But the result is not, it uses copy constructor. I searched many and the closest one is saying ``it's not guarenteed here for NRVO in the if-branch, even in c++17", but they didn't give reason. So, WHY? Thanks everyone! |
woodbrian77@gmail.com: Jul 16 07:17PM -0700 > project if we use my software as part of the project. > More info here: http://webEbenezer.net/about.html > . The Bloomberg.com headline reads: Netflix Plunges on Dim Subscriber Outlook. Unfortunately, for Netflix, it's not a free service. It makes me wonder how much longer until Bloomberg reports: Ebenezer Soars on Strong User Growth. Brian Ebenezer Enterprises - Enjoying programming again. https://webEbenezer.net |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jul 17 03:32PM +0100 > Unfortunately, for Netflix, it's not a free service. > It makes me wonder how much longer until Bloomberg > reports: Ebenezer Soars on Strong User Growth. Not only are you a bigot, Brian, you are a deluded bigot. /Flibble -- "Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin "You won't burn in hell. But be nice anyway." – Ricky Gervais "I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais "Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?" "I'd say, bone cancer in children? What's that about?" Fry replied. "How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil." "Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say." |
woodbrian77@gmail.com: Jul 17 07:50AM -0700 On Friday, July 17, 2020 at 9:32:22 AM UTC-5, Mr Flibble wrote: > > It makes me wonder how much longer until Bloomberg > > reports: Ebenezer Soars on Strong User Growth. > Not only are you a bigot, Brian, you are a deluded bigot. https://www.dailywire.com/episode/ep-1054-what-if-there-are-no-good-answers Deluded for taking on big companies? I have to hand it to Duckduckgo for their search engine. They are standing up to some big companies with aplomb. I've been using them for decades and they are doing a good job. Now Netflix stumbles. I'm shocked. Haha, not really. |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jul 17 05:54PM +0100 > I've been using them for decades and they are doing a > good job. Now Netflix stumbles. I'm shocked. Haha, > not really. Do you have a fixed list of things you talk about on Usenet? It seems every third post almost as regular as clockwork you spam about Duckduckgo, almost like a bot. Your other posts all seem to be on a rota too; you change them slightly each time changing the order of words and such but the content is identical. Are you brain damaged in some way? /Flibble -- "Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin "You won't burn in hell. But be nice anyway." – Ricky Gervais "I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais "Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?" "I'd say, bone cancer in children? What's that about?" Fry replied. "How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil." "Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say." |
Juha Nieminen <nospam@thanks.invalid>: Jul 17 09:02AM >> I see exactly what you are doing. It's useless to keep pretending. > Trolling you! Nah, I don't think he is. Trolling (as that word originally meant in internet slang) would mean that he's not seriously making the claims he is, and instead he's just making stuff up for the sole purpose of riling people up, for his own amusement. A kind of online forum vandalism. I don't get that impression. I think he's genuinely trying to weasel himself out of having to admit any sort of mistake or error, probably because of some kind of fear of making himself look like a fool, or something. If he *is* actually trolling, he is quite good at it. He's fooling me at least. |
David Brown <david.brown@hesbynett.no>: Jul 17 11:10AM +0200 On 17/07/2020 11:02, Juha Nieminen wrote: > look like a fool, or something. > If he *is* actually trolling, he is quite good at it. He's > fooling me at least. I think you are probably technically correct - people are quick to use the terms "troll" and "spam" in very loose terms. But whatever you want to call it, it is clearly a totally pointless thread. It has mostly descended into name-calling (you've been polite, others haven't), and it's going round in circles. You will never persuade this guy, and he will not stop. All that happens is that the lot of you annoy more members of the group. Give it up as a lost cause. Let the rest of the group judge whose opinions, experiences and knowledge they respect - and don't lose that respect by getting bogged down in threads like this one. |
boltar@nowhere.co.uk: Jul 17 02:42PM On Fri, 17 Jul 2020 09:02:03 +0000 (UTC) >weasel himself out of having to admit any sort of mistake or >error, probably because of some kind of fear of making himself >look like a fool, or something. Oh the irony :) |
Jorgen Grahn <grahn+nntp@snipabacken.se>: Jul 17 06:12AM On Fri, 2020-07-10, Alf P. Steinbach wrote: ... > The offset is due to the higher version number going all the way back to > Lattice C, the compiler that Microsoft built on for their first Visual C > (hence, compiler name "cl.exe", or so I believe), FWIW, the Lattice C compiler was named "lc", at least on the Amiga. When it became SAS C, it was renamed "sc" there, according to my old Makefiles. > while the lower > version number only goes back to the introduction of Visual C++. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment