- Best way to use enum with classes - 20 Updates
- Stripping cast from macro - 1 Update
- No small-vector optimization anymore? - 3 Updates
- Read the Bible - 1 Update
JiiPee <no@notvalid.com>: Oct 14 02:00AM +0100 This one I have been pondering that what is the best way. Say we have a class and we use it like this: ################ class SpecialVehicle { public: enum Direction { North, East, South, West }; void setDirection(Direction dir) { m_curDirection = dir; } private: Direction m_curDirection; }; int main() { SpecialVehicle veh; veh.setDirection(SpecialVehicle::East); return 0; } ############### Now enum can be implemented in 3 different ways: 1) (above): class SpecialVehicle { public: enum Direction { North, East, South, West }; ... with a call: veh.setDirection(SpecialVehicle::East); 2) (new enum) class SpecialVehicle { public: enum class Direction { North, East, South, West }; ... with a call: veh.setDirection(SpecialVehicle::Direction::East); 3) (new enum outside the class) enum class Direction { North, East, South, West }; class SpecialVehicle { public: ... with a call: veh.setDirection(Direction::East); So which is the best practise? They all have their advantages: - 1) is bundled inside the class (+) , has short call (+) but is not so safe (old enum) (-) - 2) is bundled inside the class (+) , has long call (-) and is safe (new enum) (+) - 3) is not bundled inside the class (-) , has short call (+) and is safe (new enum) (+) |
Jerry Stuckle <jstucklex@attglobal.net>: Oct 13 09:08PM -0400 On 10/13/2016 9:00 PM, JiiPee wrote: > (new enum) (+) > - 3) is not bundled inside the class (-) , has short call (+) and is > safe (new enum) (+) What data members and methods would class Direction have? Do you have another Direction class with something else (i.e. Northeast, Southeast, etc.)? Is Direction used in any other class? Take by itself there is no "best practice". It depends on the program as a whole. -- ================== Remove the "x" from my email address Jerry Stuckle jstucklex@attglobal.net ================== |
JiiPee <no@notvalid.com>: Oct 14 02:25AM +0100 On 14/10/2016 02:08, Jerry Stuckle wrote: > What data members and methods would class Direction have? Do you have > another Direction class with something else (i.e. Northeast, Southeast, > etc.)? Is Direction used in any other class? Direction is not a class.. its an enum... |
Jerry Stuckle <jstucklex@attglobal.net>: Oct 13 09:49PM -0400 On 10/13/2016 9:25 PM, JiiPee wrote: >> another Direction class with something else (i.e. Northeast, Southeast, >> etc.)? Is Direction used in any other class? > Direction is not a class.. its an enum... No, it's another option I supplied. But you didn't answer my questions. -- ================== Remove the "x" from my email address Jerry Stuckle jstucklex@attglobal.net ================== |
JiiPee <no@notvalid.com>: Oct 14 03:28AM +0100 On 14/10/2016 02:49, Jerry Stuckle wrote: > No, it's another option I supplied. I still dont quite understand. I have a class: class SpecialVehicle which uses Direction enum. Can you type an example what you mean? |
Jerry Stuckle <jstucklex@attglobal.net>: Oct 13 11:21PM -0400 On 10/13/2016 10:28 PM, JiiPee wrote: > class SpecialVehicle > which uses Direction enum. > Can you type an example what you mean? You might have a class Direction which has appropriate members and use that instead of your enum. There can be advantages to that, like being able to increment the direction around the compass. But forget the class and answer my other questions. You can't look at something by itself; you have to know how it fits in with the rest of the program. -- ================== Remove the "x" from my email address Jerry Stuckle jstucklex@attglobal.net ================== |
Paavo Helde <myfirstname@osa.pri.ee>: Oct 14 08:49AM +0300 On 14.10.2016 4:00, JiiPee wrote: > (new enum) (+) > - 3) is not bundled inside the class (-) , has short call (+) and is > safe (new enum) (+) 4) Put the enum in a namespace outside of the class: namespace SpecialVehicleConstants { enum class Direction { North, East, South, West }; } Then the code can choose if it wants to use longer or shorter names (with 'using namespace' or namespace aliases). Yes, new class-style enums are safer and should be usually preferred. Apart of this technical detail, use the form which most clearly expresses the intent. If it is really specific to the class, define it in the class. If the enum also makes sense without the class, define it outside. Especially, if the enum is used by other functions or classes which do not otherwise require definition of class SpecialVehicle, then define it outside of the class in a separate header which can be included without including SpecialVehicle.h. This reduces dependencies and increases modularity. If the names are too long, just use shorter names: SpecVehicle::Dir::East; Too long names reduce readability (the same holds for too short names as well, of course). The '::' separators make long names a bit easier to read, but not infinitely. hth Paavo |
4ndre4 <626401fd-4bc7-4f8f-b641-563ae0f72920@626401fd-4bc7-4f8f-b641-563ae0f72920.invalid>: Oct 14 07:52AM +0100 On 14/10/2016 02:00, JiiPee wrote: [...] > public: > enum Direction { North, East, South, West }; The concept of direction is a general and generic one, not class-specific. It may be needed in other contexts as well. You should define it outside the class. -- 4ndre4 |
JiiPee <no@notvalid.com>: Oct 14 11:07AM +0100 On 14/10/2016 04:21, Jerry Stuckle wrote: > You might have a class Direction which has appropriate members and use > that instead of your enum. There can be advantages to that, like being > able to increment the direction around the compass. oh *instead* of using enum using class. But I was only thinking of using enum this time... For example: enum Month {Jan, Feb, March} ... then I dont think there is any reason to use class there. So enum is many times best. |
JiiPee <no@notvalid.com>: Oct 14 11:10AM +0100 On 14/10/2016 02:08, Jerry Stuckle wrote: > What data members and methods would class Direction have? Do you have > another Direction class with something else (i.e. Northeast, Southeast, > etc.)? Is Direction used in any other class? No, lets say we dont have any other directions... it can only ever move to 4 directions. Like moving in a grid. Its possible another kind of Direction enum would be in another class in the future, yes. That option must be left open. |
JiiPee <no@notvalid.com>: Oct 14 11:19AM +0100 On 14/10/2016 06:49, Paavo Helde wrote: > } > Then the code can choose if it wants to use longer or shorter names > (with 'using namespace' or namespace aliases). sure > Yes, new class-style enums are safer and should be usually preferred. even if we need to then type longer names? like SpecialVehicle::Dir::East ? So we should never think about the shortness of names used in the code? Becouse thats why I used the old style this time (so that I can only write SpecialVehicle::East ) ... as it was inside the class and also used outside the class. > expresses the intent. If it is really specific to the class, define it > in the class. If the enum also makes sense without the class, define > it outside. ok, this makes sense. Although sometimes difficult to know if its only specific for the class and not outside world. > it outside of the class in a separate header which can be included > without including SpecialVehicle.h. This reduces dependencies and > increases modularity. ok, this was a good point and good to know. > If the names are too long, just use shorter names: > SpecVehicle::Dir::East; In this case I am just a bit scared somebody would confuse Dir with "Directory" :) |
JiiPee <no@notvalid.com>: Oct 14 11:20AM +0100 On 14/10/2016 07:52, 4ndre4 wrote: > The concept of direction is a general and generic one, not > class-specific. It may be needed in other contexts as well. You should > define it outside the class. Yes I think you right here. But what if it is specific to the class only? Would you always use the latest class-type enum and would you put it inside the class or outside? |
JiiPee <no@notvalid.com>: Oct 14 11:29AM +0100 if its a generic , like: enum class Direction { North, East, South, West }; should this always (automatically) be put into some global space becouse its not specific for any class? So Direction should never be put inside a class, but rather in its own file? Thats what 4ndere4 said. Should all generic type of enums be put to global space? Like: enum Month {Jan, Feb...}? Is there a situation you would put Month inside a class (in a case lets say Month is always fixed and has those 12 months only)? |
"Öö Tiib" <ootiib@hot.ee>: Oct 14 04:12AM -0700 On Friday, 14 October 2016 04:00:57 UTC+3, JiiPee wrote: > This one I have been pondering that what is the best way. Usually, when they say that then they actually search what is the universal, "silver bullet" way. It does not exist. > (new enum) (+) > - 3) is not bundled inside the class (-) , has short call (+) and is > safe (new enum) (+) Seems that here are two questions mixed. One is about nested or not nested types. I use nested type when it is specific to the nesting class only. Otherwise I use external types. With 'using' or 'typedef' I can bring alias of external type into scope of class. Other question seems to be about if to use 'enum' or 'enum class'. That is matter of taste. I prefer the 'enum class' unless I need to interface with C. Also I do not define enum like you did. I define enum like that: enum Direction { Direction_North, Direction_East, Direction_South, Direction_West }; I define enum class like that: enum class Direction { North, East, South, West }; |
JiiPee <no@notvalid.com>: Oct 14 12:18PM +0100 On 14/10/2016 12:12, Öö Tiib wrote: > I use nested type when it is specific to the nesting > class only. so becouse Direction is not specific only for a class you would put it outside the class? |
4ndre4 <a.laforgia@gmail.com>: Oct 14 05:15AM -0700 On Friday, 14 October 2016 12:12:13 UTC+1, Öö Tiib wrote: [...] > Direction_South, > Direction_West > }; With all due respect, this is horrible :) |
Jerry Stuckle <jstucklex@attglobal.net>: Oct 14 08:17AM -0400 On 10/14/2016 6:07 AM, JiiPee wrote: > enum this time... > For example: enum Month {Jan, Feb, March} ... then I dont think there is > any reason to use class there. So enum is many times best. Yes, it can be best. But that depends on the needs of the rest of your program, not just one small piece. -- ================== Remove the "x" from my email address Jerry Stuckle jstucklex@attglobal.net ================== |
Jerry Stuckle <jstucklex@attglobal.net>: Oct 14 08:18AM -0400 On 10/14/2016 6:10 AM, JiiPee wrote: > to 4 directions. Like moving in a grid. > Its possible another kind of Direction enum would be in another class in > the future, yes. That option must be left open. In that case you should have it as a global enum or possibly a class itself. -- ================== Remove the "x" from my email address Jerry Stuckle jstucklex@attglobal.net ================== |
4ndre4 <a.laforgia@gmail.com>: Oct 14 05:16AM -0700 On Friday, 14 October 2016 11:21:02 UTC+1, JiiPee wrote: [...] > Yes I think you right here. But what if it is specific to the class > only? It's a matter of scope: - does it need to be known outside the class? Yes -> outside. No -> inside. |
JiiPee <no@notvalid.com>: Oct 14 01:22PM +0100 On 14/10/2016 13:18, Jerry Stuckle wrote: >> Its possible another kind of Direction enum would be in another class in >> the future, yes. That option must be left open. > In that case you should have it as a global enum or possibly a class itself. So your logic is here that is the class is the only one who use the enum then put it inside the class.. otherwise outside the class (if somebody outside the class needs to use it)? |
mark <mark@invalid.invalid>: Oct 14 12:11PM +0200 Something that works with all versions of GCC, Clang and Intel C++ (that have C++11 / constexpr support): #define REGISTER (*(volatile uint32_t *)0x42424242) #define FORCE_CONST(x) (__builtin_constant_p(x) ? (x) : (x)) #define FORCE_CONST_ADDR(x) FORCE_CONST(reinterpret_cast<uintptr_t>(&x)) constexpr uintptr_t test = FORCE_CONST_ADDR(REGISTER); |
woodbrian77@gmail.com: Oct 13 06:58PM -0700 On Thursday, October 13, 2016 at 6:12:53 PM UTC-5, red floyd wrote: > > took great care to have a small-string optimization. So, how > > do you store an array of two double values in modern C++? > > I made up the following code. What do you think about it? That sounds like this talk: https://www.youtube.com/watch?v=vElZc6zSIXM&spfreload=1 He talks about it in the first ten minutes I think and then there's a question at the end about it also. > > int main() > > { ::std::basic_string< double > s{ 1.2, 3.6 }; > > ::std::cout << s.at( 0 )<< '\n'; } That's twisted. > for (auto d : arr) > ::std::cout << d << '\n'; > } That's better. Brian Ebenezer Enterprises http://webEbenezer.net |
Gareth Owen <gwowen@gmail.com>: Oct 14 07:31AM +0100 > Boost has a small-vector optimization container; I have also created > one (neolib::vecarray). The LLVM libray has one also. |
Gareth Owen <gwowen@gmail.com>: Oct 14 07:33AM +0100 > for (auto d : arr) > ::std::cout << d << '\n'; > } Yes, but you can't dynamically resize it later. |
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Oct 13 05:36PM -0700 Things that won't get you into Heaven: Good works Baptism Church Religion Sunday School Charity Money Mass Communion Parents Political association Denomination Friends Positive thinking Being a "good person" ----- Things that will get you into Heaven: Jesus ----- Irreducible complexity. It's not only easier, it's the correct answer. Best regards, Rick C. Hodgin |
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