Author Topic: Visual Studio so close to awesome  (Read 19154 times)

chooseusername

  • Rogueliker
  • ***
  • Posts: 329
  • Karma: +0/-0
    • View Profile
    • Email
Visual Studio so close to awesome
« on: June 30, 2015, 08:05:12 AM »
I was working on my Roguelike engine and something I've made use of is anonymous structs and unions, which come as part of C11.

Code: [Select]
struct tagged {
    unsigned int top_type;
    struct {
        int b;
    };
    struct {
        unsigned int key_type;
        union {
            char c;
            char text[32];
            unsigned int keycode;
        };
    };
};
All struct and union members accessible on the same flattened level.  Now you might wonder what happens if you want to share a part of this in two locations.  You could make a #define and hack it in place anonymously, or you could take advantage of a custom microsoft extension to C (also available in GCC).
Code: [Select]
union shared_key_value_u {
    char c;
    char text[32];
    unsigned int keycode;
};

struct shared_key_entry_s {
unsigned int key_type;
union shared_key_value_u;
};

struct location1_s {
    unsigned int top_type;
    struct {
        int b;
    };
    struct shared_key_entry_s;
};

union location2_u {
    struct shared_key_entry_s;
    unsigned int v1;
    struct {
        int aa;
        char cc;
    };
};
This allows me to write much nicer code.  Honestly, I think it should have been part of the C11 standard.  It's portable anyway, as you can pass "-fms-extensions" to GCC.

The only problem is that Intellisense doesn't implement the same grammar as the VS compiler, so it chokes and claims an error on every single member reference that compiles without error.  Which, floods your editor view with red incorrect noise.

Such a pity.  The weird thing is that Intellisense will work, if you limit your usage of this feature, perhaps to 'struct location1_s' or 'struct shared_key_entry_s'.  But if you throw in the 'union location2_u' case, then all the other cases now choke in Intellisense where they didn't before.  So annoying.

I hope they fix this in VS2015 which is coming out in a month.

Cfyz

  • Rogueliker
  • ***
  • Posts: 194
  • Karma: +0/-0
    • View Profile
    • Email
Re: Visual Studio so close to awesome
« Reply #1 on: June 30, 2015, 10:12:20 AM »
Being rather subjective, I am curious if there is a good reason using these darker sides of C in something like roguelike engine. Unions make data strcture less obvious and introduce possibilities for subtle bugs. It's justified in bare-metal situations or when trying to process something on a scale of millions of items per second. But roguelike?

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Visual Studio so close to awesome
« Reply #2 on: June 30, 2015, 02:02:58 PM »
It looks like you are trying to do something "smart". Usually when you start to stretch the language it's not going to give you any real advantages and will probably give you only problems, if not immediately then later.

chooseusername

  • Rogueliker
  • ***
  • Posts: 329
  • Karma: +0/-0
    • View Profile
    • Email
Re: Visual Studio so close to awesome
« Reply #3 on: June 30, 2015, 09:47:10 PM »
Being rather subjective, I am curious if there is a good reason using these darker sides of C in something like roguelike engine. Unions make data strcture less obvious and introduce possibilities for subtle bugs. It's justified in bare-metal situations or when trying to process something on a scale of millions of items per second. But roguelike?
It's modern standardised C, and a Microsoft extension of that which they use themselves in their headers.  darker?

You're really missing out.  I find it to be a natural extension of C that makes it easier to do what I want without preprocessor hanky panky.  If someone gets confused about combinations of structs and unions, honestly, they're not a very good programmer, or they've named them badly.  It makes code more straightforward and cleaner when done right.  If someone would get the use of them wrong, I worry about their use of pointers, and things like pointer arithmetic.

chooseusername

  • Rogueliker
  • ***
  • Posts: 329
  • Karma: +0/-0
    • View Profile
    • Email
Re: Visual Studio so close to awesome
« Reply #4 on: June 30, 2015, 09:49:49 PM »
It looks like you are trying to do something "smart". Usually when you start to stretch the language it's not going to give you any real advantages and will probably give you only problems, if not immediately then later.
I'm using straightforward natural feeling modern C and my code is cleaner for it.  I'm hitting a bug in Microsoft's editor.  Trying to do something "smart" is programming in C++  ;)

Cfyz

  • Rogueliker
  • ***
  • Posts: 194
  • Karma: +0/-0
    • View Profile
    • Email
Re: Visual Studio so close to awesome
« Reply #5 on: July 01, 2015, 12:52:21 AM »
By darker I meant more rare, somewhat complex, error-prone even. Explicit memory order of atomic operations is also modern standardised C/C++ but using it for ad-hoc synchronization without a good reason and whole lot of understanding is plain wrong. I am under impression you indeed understand what you're doing and therefore curious about the reason. I've seen a fair amount of C code and the only reasonable use for unioning was close-to-metal operation. Heavily constrained code, interfacing with hardware modules with particular layout quirks and all that stuff. Since it almost always came to jumping through code and documentation figuring what goes where, I came to conclusion this is somewhat like volatile, which is indispensable in its very narrow use case, but totally misunderstood and horribly misused in modern desktop code.

Don't intend to start some flame, just can't see how mashing several loosely related things into one can help in arguably high-level (as opposed to MCU programming) roguelike world. Would be nice to see some example =)
« Last Edit: July 01, 2015, 12:54:06 AM by Cfyz »

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Visual Studio so close to awesome
« Reply #6 on: July 01, 2015, 08:12:48 AM »
Trying to do something "smart" is programming in C++  ;)

I like how that comment tells everything about your problems on which you yourself have no idea. Even "normal" C roguelike source codes tend to get very messy after they reach major level. What do you think will happen when you start to play with esoteric stuff like that? Good luck, man.

chooseusername

  • Rogueliker
  • ***
  • Posts: 329
  • Karma: +0/-0
    • View Profile
    • Email
Re: Visual Studio so close to awesome
« Reply #7 on: July 04, 2015, 01:02:29 AM »
By darker I meant more rare, somewhat complex, error-prone even. Explicit memory order of atomic operations is also modern standardised C/C++ but using it for ad-hoc synchronization without a good reason and whole lot of understanding is plain wrong. I am under impression you indeed understand what you're doing and therefore curious about the reason. I've seen a fair amount of C code and the only reasonable use for unioning was close-to-metal operation. Heavily constrained code, interfacing with hardware modules with particular layout quirks and all that stuff. Since it almost always came to jumping through code and documentation figuring what goes where, I came to conclusion this is somewhat like volatile, which is indispensable in its very narrow use case, but totally misunderstood and horribly misused in modern desktop code.

Don't intend to start some flame, just can't see how mashing several loosely related things into one can help in arguably high-level (as opposed to MCU programming) roguelike world. Would be nice to see some example =)
I am just venting about an unfortunate bug that makes editing code that is both valid and supported by Microsoft impossible.  Hopefully it's fixed in VS2015.

You've made an assumption about union.  I don't know why.  I can't sit here and defend it, as I think that I shouldn't need to.  The benefits are obvious in my code, for the readability it adds. And being straightforward and readable is my main goal in my code.  All I can do is hope that some day you'll try using it, and judge it on it's merits.

It's unfortunate when people imagine problems with things, and then proceed to discourage their use.  The worst is the "goto considered harmful" meme.

chooseusername

  • Rogueliker
  • ***
  • Posts: 329
  • Karma: +0/-0
    • View Profile
    • Email
Re: Visual Studio so close to awesome
« Reply #8 on: July 04, 2015, 01:06:23 AM »
Trying to do something "smart" is programming in C++  ;)

I like how that comment tells everything about your problems on which you yourself have no idea. Even "normal" C roguelike source codes tend to get very messy after they reach major level. What do you think will happen when you start to play with esoteric stuff like that? Good luck, man.
To paraphrase: My comment tells an undefined everything.  Code sometimes gets messy as it evolves.  A rhetorical question based on unclear statements?  Not sure I understand.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Visual Studio so close to awesome
« Reply #9 on: July 04, 2015, 02:24:42 PM »
Not sure I understand.

Two things likely to happen. 1. You will understand, 2. You quit programming your roguelike.

Cfyz

  • Rogueliker
  • ***
  • Posts: 194
  • Karma: +0/-0
    • View Profile
    • Email
Re: Visual Studio so close to awesome
« Reply #10 on: July 05, 2015, 05:31:27 PM »
Quote from: chooseusername
You've made an assumption about union.  I don't know why.  I can't sit here and defend it, as I think that I shouldn't need to. <...> All I can do is hope that some day you'll try using it, and judge it on it's merits.
Evidently Krice has taken his toll on the forum. I did state that I do not want flame and I did state I believe you are know what you're doing (credibility based on your other posts) but do not understrand why and therefore curious. "I do not care if you mortals do not understand" was uncalled for but kind of ironic taking into account thread participants.

Quote from: chooseusername
The benefits are obvious in my code, for the readability it adds. And being straightforward and readable is my main goal in my code.
Would be nice to see an example since I cannot imagine how it can be more straightforward and readable than plain structures and, maybe, a bit of polymorphism (I am not talking about C++ here).
« Last Edit: July 05, 2015, 05:36:04 PM by Cfyz »

chooseusername

  • Rogueliker
  • ***
  • Posts: 329
  • Karma: +0/-0
    • View Profile
    • Email
Re: Visual Studio so close to awesome
« Reply #11 on: July 05, 2015, 07:49:42 PM »
Evidently Krice has taken his toll on the forum. I did state that I do not want flame and I did state I believe you are know what you're doing (credibility based on your other posts) but do not understrand why and therefore curious. "I do not care if you mortals do not understand" was uncalled for but kind of ironic taking into account thread participants.
Why do I need to convince you of this?  Perhaps you should read it as, I don't have the time or interest to sit here and construct time consuming posts to defend it.  You admit you haven't tried it.  It's common sense that when you do, if you do, that you might gain a different perspective.  I am not here to change your mind, just to vouch that it has potential.

Cfyz

  • Rogueliker
  • ***
  • Posts: 194
  • Karma: +0/-0
    • View Profile
    • Email
Re: Visual Studio so close to awesome
« Reply #12 on: July 05, 2015, 08:05:37 PM »
No, I did not admit such things. I said I've seen it in a few projects. Also, I've used this particular feature myself (which is kind of obvious). Which did gave me some perspective. You do not need to convince me of anything really, I was kind of asking a favor, prying into this out of curiosity. Well, no biggie, I won't bother you on this any further.

TheCreator

  • Rogueliker
  • ***
  • Posts: 370
  • Karma: +0/-0
    • View Profile
    • Fame
    • Email
Re: Visual Studio so close to awesome
« Reply #13 on: July 06, 2015, 06:29:50 AM »
Can we see an example that shows how these weird constructs allow you to write better code? I have seen things like that before, but I connect them to a rather bad style of programming. Of course I might be wrong on that as I use different languages and different paradigms. Honestly I don't see anything particularly useful in these bits of code, but an example could be enlightening. That way the topic would not transform into a pointless flame war.

By the way, I've been using Visual Studio for about 10 years and learned one thing; you can experiment with syntax, but if the IDE crashes, it means that you've gone too far ;).
Fame (Untitled) - my game. Everything is a roguelike.