4
Lake
3y

Programmer: Type type(2);
C++ Compiler: // Okay, I'll use the constructor that takes 1 argument
Programmer: Type type();
C++ Compiler: // I see there is a constructor that takes no arguments, but surely you don't want that. Everybody loves functions, a function shall be declared!

Who, in their right mind, thought this syntax is a good idea?! Syntax inconsistencies drive me crazy...

Comments
  • 1
    Just do not use any parenthesis, dude...
  • 0
    @iiii Imagine if char[] was a array of 1 bytes but char was 2 bytes. Your comment is like saying "just don't use char, use int8_t, dude..."
  • 1
    @Lake eh? Those are totally different things.
  • 0
    @iiii I'm not asking how to use a constructor, I'm saying it's bad inconsistent design to have "Type(args)" mean one thing if args is empty but another if it's not. It would be similar to saying char is int8_t when used with an array of size 1, but int16_t if it's used with array of size bigger than 1.
  • 1
    @Lake you're not supposed to call a default constructor unless you're using "new". It's called default for a reason.
  • 0
    Let me introduce you to these things called braces.
  • 0
    And FYI, "devrant" is for rants about the app. For future reference, this would be a rant.
  • 1
    Basically you want Type type{}; and Type type{args}; which is modern c++ anyway.
  • 0
    This rant can be summed up as "Writes bad code. Complains code is bad."

    I'll be quiet now...
  • 1
    @iiii @atheist OP is actually right. This is a documented inconsistency. It's called the "most vexing parse". Look it up. The compiler is required to interpret that as a function.

    How C++ *should* be used is irrelevant here - that is legitimate syntax for object construction. The syntax allows the ambiguity to exist though, so they had to arbitrarily choose of the two interpretations as default.
  • 0
    @RememberMe ++++

    @Lake
    C++ is a funny language but it has its charms. 🙃
Add Comment