TIL that in C++, you can declare variable in if statement



  • And by statement, I mean the condition.

    Real code I've just encountered, anonymized:

    if (auto type = enum_converter::get_type_of_this_thing(elem.first.type)) {
    	auto my_thing = im::thing(elem.first.data.get(), elem.second, type.get());
    	my_list.insert(my_thing);
    }
    

  • sockdevs

    ..... okay....

    is this a defined thing or are we in nasal demon territory?

    if the former what is the scope of auto type?


  • :belt_onion:

    NEWS FLASH! You can also do this in a for loop! And if STL iterators are available you can even.

    for(auto thing : collectionOfThings)
    

    @accalia said:

    what is the scope of auto type

    Within the statement / loop block.


  • sockdevs

    @Onyx said:

    Within the statement / loop block.

    well then.

    that's neat.



  • Can't you do that in virtually every language? Maybe not, like, Visual Basic I guess... or HyperTalk perhaps...


  • sockdevs

    not in python. assignment is forbidden inside the conditional of an if statement.

    it's been a while since i did pyhton but i believe the same is true for the conditional parts of loops, although the loop construct does allow (encourage? require?) the introduction of a loop scoped variable for iteration.


  • :belt_onion:

    It's a bit of a new-fangled thing in C++. Relatively speaking, Not sure about the if case, but the ranged-based for loops are only available since C++11.



  • Huh. Well live and learn.

    Our C# codebase is full of stuff like:

    if( new Date().IsLastDayOfMonth() ) { // do stuff };

  • area_deu

    Where's the declaration in there?



  • In classic C, all variable declarations have to be at the start of the block, as a compilation optimization. Modern C allows declarations later on, but hoists them to the start of the block.

    .NET IL requires you to declare the number and type of all locals (variables) at the start of a function, as well as the maximum depth of the evaluation stack.

    @blakeyrat said:

    Our C# codebase is full of stuff like:
    You're not declaring a variable there, so there's no need to allocate a dedicated local slot; it can just live on the evaluation stack. The thing that all the C++ weenies are excited about is

    if( (var d = new Date()).IsLastDayOfMonth() { /* do stuff with d */ }
    


  • @Onyx said:

    NEWS FLASH! You can also do this in a for loop!

    I just realized that. It's a whole new world once you realize you can declare variables on either side of first semicolon.

    for (int i = 0; int j = 1;);
    

    @Onyx said:

    And if STL iterators are available you can even. for(auto thing : collectionOfThings)

    Note there is no condition statement here.



  • Seriously? Or...?

    new Date() declares a new Date object. (Our company has its own date object, because 1) we generally don't need time like the C# libraries assume we do, and 2) so we can load it up with industry-specific helpers.)

    I just don't bother giving it a name. Unless I'd need it later on in the function.

    @TwelveBaud said:

    You're not declaring a variable there, so there's no need to allocate a dedicated local slot; it can just live on the evaluation stack.

    What-fucking-ever. I'm not a pedantic dickweed. To me the only difference between these two VARIABLE DECLARATIONS:

    new Date();
    
    <poo>
    var blah = new Date();
    

    Is that one has a name and one doesn't.

    AND IN ANY CASE both work inside an if() in C# so it's not even NECESSARY pedantic dickweedery. Christ.

    @Gaska said:

    Note there is no condition statement here.

    It's an iterator, right? Iterators always return true if they can be iterated, false if they cannot... right? Am I crazy here?



  • @blakeyrat said:

    new Date() declares a new Date object.

    No, it creates new Date object.

    @blakeyrat said:

    I just don't bother giving it a name.

    Moreover, you don't bother to even store it in any variable (why would you?).



  • Declaring a variable in an if expression is the correct way to handle a file stream:

    if(std::ifstream in {"file.txt"})
    {
        //do stuff with file
    }
    else
    {
        std::cerr << "Couldn't open file.txt" << std::endl;
    }
    

    Because RAII is a good thing and you don't want to pollute the enclosing scope with an unusuable variable.


  • :belt_onion:

    Yeah, but that's doable in ANSI C too, declaring variables is not. Well, ok, you can't really instantiate a class since there's no such thing in C, but you can call a function or a value to an already declared variable.

    Declaring a new variable is a whole different can of worms in C.



  • @Gaska said:

    No, it creates new Date object.

    Goddamnit. How do people like you go about daily life without being punched in the face every 25 minutes?

    "Here's my bus pass, driver." "Uh, actually, it's a bus token. We don't use bus passes here. We have transfers and tokens but there's no pass. You must be thinking of Disneyland and not the bus! Ha ha ha ha--" *punch in face*



  • @blakeyrat said:

    It's an iterator, right? Iterators always return true if they can be iterated, false if they cannot... right? Am I crazy here?

    You are crazy. Iterators don't convert to bool.



  • It's been a LONG time since I did any C++, but I seem to recall C++ iterators could be checked against bool using operator overloading or something. But, who cares, C++ is a shitty language which sucks and the pedantic dickweedery has pissed me off in this topic.


  • area_deu

    @blakeyrat said:

    VARIABLE DECLARATIONS:

    Please read up on what a declaration is.
    The C# spec is commonly included with Visual Studio.



  • @Gaska said:

    @Onyx said:
    And if STL iterators are available you can even. for(auto thing : collectionOfThings)

    Note there is no condition statement here.

    The range-based for is syntactic sugar for this:

    for (auto iter = collectionOfThings.begin(); iter != collectionOfThings.end(); ++iter)
    


  • In C++1z you don't even need auto in the range based for loop:

    for(v : values)

    They're called terse range-based for-loops.



  • @blakeyrat said:

    Goddamnit. How do people like you go about daily life without being punched in the face every 25 minutes?

    "Here's my bus pass, driver." "Uh, actually, it's a bus token. We don't use bus passes here. We have transfers and tokens but there's no pass. You must be thinking of Disneyland and not the bus! Ha ha ha ha--" punch in face


    I wonder how YOU go about daily life without being punched in the face every 25 minutes.

    "It's 25 dollars."
    *hands in 25 cents*
    "Um, sir... This isn't 25 dollars, it's 25 cents."
    "GOSH DARNIT YOU ASKED FOR 25 AND I GAVE YOU 25 WHATS YOUR PROBLEM ARRRGGHHHHHH"



  • @LB_ said:

    In C++1z you don't even need auto in the range based for loop

    Suddenly I like it much, much less.



  • @blakeyrat said:

    AND IN ANY CASE both work inside an if() in C# so it's not even NECESSARY pedantic dickweedery.
    Jesus Christ, things have names for reasons! The first one isn't a declaration. If it was, it wouldn't be valid in a C# if().@C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Specifications\ 1033\CSharp Language Specification.docx said:
    A local variable declaration specifies a type name, a variable name, and possibly an initial value. [...] A local variable is declared by a local-variable-declaration, which may occur in a block, a for-statement, a switch-statement or a using-statement; or by a foreach-statement or a specific-catch-clause for a try-statement.



  • @Gaska said:

    Suddenly I like it much, much less.

    Why?



  • @aliceif said:

    Please read up on what a declaration is.The C# spec is commonly included with Visual Studio.

    I know the pedantic dickweedery you people are referring to. What I don't get is how it affects the actually meaning of my posts in this topic.

    The reason for that is, IT FUCKING DOESN'T.

    Yes, I used the wrong word. Whoop-de-shit. My point still applies. So there's no need for the pedantic dickweedery. If you want to feel smarter by correcting me on irrelevant points of terminology, well, then kudos to you-- but you don't have to type and post it here, ok? Because it just makes you into a dickweed who deserves a punch in the face.



  • I'm going to have to insist that there's a non-trivial difference between declaring a variable and calling a constructor. In fact, they're orthogonal concepts. You can declare a variable without calling a constructor and you can call a constructor without declaring a variable.

    Your analogy should look more like this:

    @blakeyrat said:

    "Here's my bus pass, driver." "Uh, actually, it's a coupon for laundry detergent. Are you a fucking retard or something?"



  • @Gaska said:

    @LB_ said:
    In C++1z you don't even need auto in the range based for loop

    Suddenly I like it much, much less.

    But it saves you from typing 7 characters!


  • :belt_onion:

    @NedFodder said:

    The range-based for is syntactic sugar for this:

    Which I have been writing before I could compile in C++11 mode. I'm lazy. I love the new way.

    @LB_ said:

    In C++1z you don't even need auto in the range based for loop:

    Huh. Cool. I usually stick the actual type in it whenever I can though: more readable and IDE's autocomplete doesn't get confused.



  • It actually does, Blakey. It's not a declaration and you can't do those in if-statements in C# which makes your point non-valid.



  • Ok, let's all pile on!

    Who cares about having a conversation when there's pedantic dickweedery to shill!!!!!



  • @blakeyrat said:

    Goddamnit. How do people like you go about daily life without being punched in the face every 25 minutes?

    Man...I thought you had to be trolling. Because your argument means that there are two variable declarations in the code below:

    Bar foo = new Bar();  // a declaration and creation
    ...
    ...
    ... foo = new Bar(); // a new declaration!
    

    ...and you can't be serious about that. Right? But now...well.



  • @Onyx said:

    more readable and IDE's autocomplete doesn't get confused.

    A couple years ago there was a plugin for sublime text that used clang to analyze the code and provide syntax hints. It worked with auto flawlessly, even in complicated cases. Modern IDEs just need to catch up to technology that existed a couple years ago.

    As for readability, there are many cases where the actual type should not be important as the focus is on how you use the type (e.g. duck typing). But that's a personal preference.



  • @blakeyrat said:

    Ok, let's all pile on!

    Well, you could stop doubling down on your mis-statement with rage. It wasn't just terminology you were wrong about.



  • @blakeyrat said:

    What I don't get is how it affects the actually meaning of my posts in this topic.

    The reason for that is, IT FUCKING DOESN'T.


    You're right. Your posts are just as dumb either way.

    @blakeyrat said:

    Yes, I used the wrong word. Whoop-de-shit. My point still applies.

    The point "every language does the same thing as C++ here"? No, it doesn't. Try to compile if (bool i = true); with your C# compiler of choice.



  • I used the wrong fucking word, get the fuck over it. Nevermind, fuck this forum.

    I guess you're all perfect angelic beings who never make mistakes, so I don't belong. Keep piling on, I won't read a word of it.



  • Honey, eat a Snickers. You're not you when you're hungry.



  • Ymbnh.


  • :belt_onion:

    @LB_ said:

    Modern IDEs just need to catch up to technology that existed a couple years ago.

    Eh. Qt Creator does a decent job otherwise. It's good with casts of any kind, but auto seems to confuse it to no end. When I can't get it to work, meh, I get by.



  • @blakeyrat said:

    What I don't get is how it affects the actually meaning of my posts in this topic.

    Yes, because your<sic> and idiot. This isn't pedantic dickweedery. It's words.

    @blakeyrat said:

    Yes, I used the wrong word. Whoop-de-shit. My point still applies.

    No, your point is a non sequitur.

    @blakeyrat said:

    Because it just makes you into a dickweed who deserves a punch in the face.



  • Sorry, I couldn't resist.



  • @blakeyrat said:

    I used the wrong fucking word, get the fuck over it. Nevermind, fuck this forum.

    I guess you're all perfect angelic beings who never make mistakes, so I don't belong. Keep piling on, I won't read a word of it.

    Even if you used the right word, it would still make your point non-valid. You used the wrong concept.



  • @Onyx said:

    It's a bit of a new-fangled thing in C++. Relatively speaking, Not sure about the if case, but the ranged-based for loops are only available since C++11.

    This version:

    [code]for( int i = 0; i < 10; ++i )
    {
    // do stuff with i
    }[/code]
    was available in C++ 98. And of course the rules about the scope of i (just during the for, or from then on until the end of the block that encloses the for) changed several times during the run-up to the release of Visual C++ 6.0 and at least once after the release, which left the poor compiler with the wrong version of those rules.

    Oh, and I found a new (to me, but it's probably known-about) way in which Discourse sucks donkey balls. There has to be a blank line between a short one-line paragraph and a ((code)) block, or the paragraph doesn't show in the preview.



  • @accalia said:

    not in python. assignment is forbidden inside the conditional of an if statement.

    Here's my contribution to the pendantry in this thread. In this for block:

    [code]for( int i = 0; i < 10; ++i )
    {
    // do stuff with i
    }[/code]

    there is no assignment. The = in int i = 0; is an initialisation, not an assignment.



  • With iterators being standard good practice, people wanted a more terse way to express the same idea.


  • :belt_onion:

    Cheers, wasn't sure it was 98 or the 2003 version. Now I know.



  • C++03 changed so little that most compilers treat it the same as C++98 or don't even mention its existence at all. Not even CMake mentions it:



  • @LB_ said:

    With iterators being standard good practice, people wanted a more terse way to express the same idea.

    Please enlighten me. What does this have to do with what I said?



  • I guess I misunderstood; I thought you were suggesting that nobody needed anything more than the basic 3-part for-loop inherited from C. Bad assumption on my part.


  • sockdevs

    @Steve_The_Cynic said:

    Here's my contribution to the pendantry in this thread.

    if you're going to bring pedantry to the table, maybe use the same language?

    I noted that the assignment is disallowed in python, your counter example is in a C like language ('m going to assume standard C as it is the oldest, the syntax matches, and most c-like syntaxes maintain the same behavior)

    However, that is not how For loops work in Python.

    they work like this.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.