Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Stylistic choices

Name: Anonymous 2011-12-13 3:18

I tend to prefer to use namespaces filled with constants, rather than using enums in most cases. For example using

namespace Element {
const uint8_t No_Element = 0;
const uint8_t Air = 1;
const uint8_t Water = 2;
const uint8_t Earth = 3;
const uint8_t Fire = 4;
};


instead of


enum Element {
No_Element, Air, Water, Earth, Fire
};


Is this bad practice?

Name: Anonymous 2011-12-13 7:03

>>40

Exactly, that's the purpose of a header file. To give you access to resources without actually having the resources.

Name: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2011-12-13 7:06

>>41
That is called "interface"

Name: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2011-12-13 7:07

Real header files  include code, templates, functions,etc
Interface headers are wrapper layers to existing functionality

Name: Anonymous 2011-12-13 7:41

>>7
LOL, type safety has its price, you need an explicit cast (int y = (int) Elements::Air;) or just use C #defines like FV.

>>8-25
Ridiculous, you're fighting for nothing. C++11 enum classs aren't common types or namespaces with const variables, and so they're treated as defined ints, but without implicit casts, and with static type and namespace checks.

Ah, FV you're a noob. Namespaces are superior than your shitty defines, because they're modular (nested), checked at compile time and don't generate a cluttered mess after preprocessor pass. The ONLY fucking disadvantage is the lack of stardard (and reasonable) name mangling.

If you all just had read >>5-6, this thread would have almost 20 less shitposts.

>>26-41
FV has a point here, but I think you mean modular programming, not OOP. And this means just to apply a divide and conquer approach to code organization, defining functions and modules. C headers are a form of modularization that can give you some reuse and black-box abstraction, but it's weak because it lacks proper scoping. Yes you can work without it, but it's easier and cleaner to write than with STD_ALGORITHM_BINARYSEARCH(v, v+v_length, cmp).
About your hungarian notation (#define EL_E 4): thank God that I don't have to read your code, I can't stand with these cryptic minified notations!

Name: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2011-12-13 8:43

>STD_ALGORITHM_BINARYSEARCH(
If i'm going to use it often would make a wrapper for convinience
#define BS STD_ALGORITHM_BINARYSEARCH

Name: Anonymous 2011-12-13 9:23

>>45
Your source is bloated as fuck.

Name: Anonymous 2011-12-13 12:48

FrozenVoid telling people to abuse the preprocessor

Please Frozenshit, gtfo of prog. No one like you and your self-taught lazy C programming experience. You barely know C allow, you write shitty code and 90% of your posts make no sense because you have not a clue what you're doing.


Fuck off troll

Name: Anonymous 2011-12-13 14:38

i like fv even though i dont agree with him

Name: Anonymous 2011-12-13 15:00

>>48
You suck cocks, faggot touhou.

Name: Anonymous 2011-12-13 18:30

i love fv even though he doesnt agree with me

Name: Anonymous 2011-12-13 18:35

>>1

Constants cannot be used in switch statements. Enumerations are provided mostly for that purpose. Therefore, replacing enumerations with constants are not a good choice. It is not simply a stylistic decision.

If you're worried with namespace pollution, enclose your enumeration in a namespace.

Name: Anonymous 2011-12-13 18:42

>>51
Constants can't be used in switch

are C++ compilers that stupid still?

I thought by now any modern compiler knows how to deal with constants when it comes to switch

Name: Anonymous 2011-12-13 19:21

>>52
If the compiler can prove a value won't be modified, it doesn't need const to tell it.

Name: Anonymous 2011-12-13 20:02

>>52

C++ compilers are smart. The stupidity in this regard is in the language: it's forbidden.

Name: Anonymous 2011-12-13 20:09

>>54
Why is that stupid?

Name: Anonymous 2011-12-13 20:37

>>42

Header (.h) = Interface
Source (.c) = Implementation

That's how it's always done.

Name: Anonymous 2011-12-14 4:43

>>52

Constants cannot be used in switch statements

OP here, that's objectively wrong and you know it. I've got a 187 line of fucking code function that uses constants in what is effectively a giant fucking switch statement. I just compiled it again to see if it worked. It does, perfectly.

Name: Anonymous 2011-12-14 9:43

>>57
I've got a 187 line of fucking code function that uses constants in what is effectively a giant fucking switch statement.
Seriously?

Name: Anonymous 2011-12-14 10:38

>>57

Well, indeed there is a case when it works: when the constant is defined in the same TU as the switch statement. For other scenarios, it doesn't work.

Consider:

extern const int i;

int main() {
    int j(0);
    switch (j) {
    case i: break;
    }
}


This code is illegal. If you remove extern, the code compiles fine, because i gets invariably defined in the file scope (with the provided value, or zero if nothing is given).

Another thing which might be considered a disadvantage over enumerations is that such constans may consume storage, albeit usually a tiny one.

Name: Anonymous 2011-12-14 10:47

>>57
I've got a 187 line of fucking code function that uses constants in what is effectively a giant fucking switch statement.
You never heard of a map?

Name: Anonymous 2011-12-14 11:55

>>56
Shit, I only have a .h and a .lib - what now?

Name: Anonymous 2011-12-14 12:02

>>61
Look for an open source alternative. If one doesn't exist, write one and make it free.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List