Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Initializer list syntax of subclass created with QtCreator class "wizard"

Initializer list syntax of subclass created with QtCreator class "wizard"

Scheduled Pinned Locked Moved Solved C++ Gurus
initializerlistc standardparenthesesbrackets
4 Posts 3 Posters 520 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Online
    P Online
    Pl45m4
    wrote on 27 Apr 2024, 16:02 last edited by
    #1

    Hey guys,

    I would say I have a solid C++ knowledge, but I'm not following every change or update to C++.
    I've noticed that the QtCreator class wizard (QtC 13 with C17) went from:

    MyWidget::MyWidget(QWidget *parent)
        : QWidget(parent){}
    

    as used in earlier versions
    to:

    MyWidget::MyWidget(QWidget *parent)
        : QWidget{parent}{}
    

    now.

    So curly brackets ( {...}) are used instead of parentheses ( (...)).

    Why that?
    Dis-/Advantages?!
    : QWidget(parent) is still valid, but should one continue to use {parent} from now on?

    Any explanation appreaciated :)


    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    J K 2 Replies Last reply 27 Apr 2024, 18:13
    0
    • P Pl45m4
      27 Apr 2024, 16:02

      Hey guys,

      I would say I have a solid C++ knowledge, but I'm not following every change or update to C++.
      I've noticed that the QtCreator class wizard (QtC 13 with C17) went from:

      MyWidget::MyWidget(QWidget *parent)
          : QWidget(parent){}
      

      as used in earlier versions
      to:

      MyWidget::MyWidget(QWidget *parent)
          : QWidget{parent}{}
      

      now.

      So curly brackets ( {...}) are used instead of parentheses ( (...)).

      Why that?
      Dis-/Advantages?!
      : QWidget(parent) is still valid, but should one continue to use {parent} from now on?

      Any explanation appreaciated :)

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 28 Apr 2024, 10:27 last edited by kshegunov
      #3

      @Pl45m4 said in Initializer list syntax of subclass created with QtCreator class "wizard":

      Why that?

      The 'feature' (debatable how much of it is really a feature) is called uniform initialization. You may want to take a peek here [1].

      : QWidget(parent) is still valid, but should one continue to use {parent} from now on?

      Depends on your style and understanding. I don't, as I don't agree that I should mindlessly slap whatever the core guideline guidelines tells me to.

      Dis-/Advantages?!

      There are cases where that syntax has unexpected behaviour. If you have say:

      class Foo
      {
          Foo();
          Foo(std::initializer_list<Bar>);
      };
      

      You might run in trouble doing:

      Foo x{}; //< We call what exactly, is this the default constructor, or the one taking an initializer list?
      

      But I will grant you, the example is contrived. Here's a case where you may hit this in Qt.

      QJsonValue a{}; //< Do we mean to call QJsonValue(), or QJsonValue(QJsonObject(std::initializer_list<...>))
      

      As it happens, due to well C++, we call the constructors that take an initializer list in both cases. All in all we shot ourselves in the foot. So, if you ask me - use {} when you mean to use an initializer list, all other cases - simply call the constructor. That is to say:

      std::vector x = { 1, 2, 3, 4 }; //< Just fine!
      std::vector<int> y(5) //< Creating a vector that has 5 elements!!
      int q = 4; //< Can't get more explicit than that. `int q(4);` and `int q{4};` are both valid, but is it clearer to read? I sure don't think so. Looks more like something you'd write to impress your colleagues. 
      

      Warning: Notice in the last code snippet that std::vector<int> y(5) is very, very different from std::vector<int> y{5}

      [1]: https://isocpp.org/wiki/faq/cpp11-language#uniform-init

      Read and abide by the Qt Code of Conduct

      P 1 Reply Last reply 28 Apr 2024, 14:10
      5
      • P Pl45m4
        27 Apr 2024, 16:02

        Hey guys,

        I would say I have a solid C++ knowledge, but I'm not following every change or update to C++.
        I've noticed that the QtCreator class wizard (QtC 13 with C17) went from:

        MyWidget::MyWidget(QWidget *parent)
            : QWidget(parent){}
        

        as used in earlier versions
        to:

        MyWidget::MyWidget(QWidget *parent)
            : QWidget{parent}{}
        

        now.

        So curly brackets ( {...}) are used instead of parentheses ( (...)).

        Why that?
        Dis-/Advantages?!
        : QWidget(parent) is still valid, but should one continue to use {parent} from now on?

        Any explanation appreaciated :)

        J Offline
        J Offline
        JonB
        wrote on 27 Apr 2024, 18:13 last edited by
        #2

        @Pl45m4
        Isn't it the same thing as "nowadays" people write int x{16}; in place of int x(16);. {}s for initializers instead of () for function call? Kind of. Now one of the experts will explain, hopefully :)

        1 Reply Last reply
        1
        • P Pl45m4
          27 Apr 2024, 16:02

          Hey guys,

          I would say I have a solid C++ knowledge, but I'm not following every change or update to C++.
          I've noticed that the QtCreator class wizard (QtC 13 with C17) went from:

          MyWidget::MyWidget(QWidget *parent)
              : QWidget(parent){}
          

          as used in earlier versions
          to:

          MyWidget::MyWidget(QWidget *parent)
              : QWidget{parent}{}
          

          now.

          So curly brackets ( {...}) are used instead of parentheses ( (...)).

          Why that?
          Dis-/Advantages?!
          : QWidget(parent) is still valid, but should one continue to use {parent} from now on?

          Any explanation appreaciated :)

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 28 Apr 2024, 10:27 last edited by kshegunov
          #3

          @Pl45m4 said in Initializer list syntax of subclass created with QtCreator class "wizard":

          Why that?

          The 'feature' (debatable how much of it is really a feature) is called uniform initialization. You may want to take a peek here [1].

          : QWidget(parent) is still valid, but should one continue to use {parent} from now on?

          Depends on your style and understanding. I don't, as I don't agree that I should mindlessly slap whatever the core guideline guidelines tells me to.

          Dis-/Advantages?!

          There are cases where that syntax has unexpected behaviour. If you have say:

          class Foo
          {
              Foo();
              Foo(std::initializer_list<Bar>);
          };
          

          You might run in trouble doing:

          Foo x{}; //< We call what exactly, is this the default constructor, or the one taking an initializer list?
          

          But I will grant you, the example is contrived. Here's a case where you may hit this in Qt.

          QJsonValue a{}; //< Do we mean to call QJsonValue(), or QJsonValue(QJsonObject(std::initializer_list<...>))
          

          As it happens, due to well C++, we call the constructors that take an initializer list in both cases. All in all we shot ourselves in the foot. So, if you ask me - use {} when you mean to use an initializer list, all other cases - simply call the constructor. That is to say:

          std::vector x = { 1, 2, 3, 4 }; //< Just fine!
          std::vector<int> y(5) //< Creating a vector that has 5 elements!!
          int q = 4; //< Can't get more explicit than that. `int q(4);` and `int q{4};` are both valid, but is it clearer to read? I sure don't think so. Looks more like something you'd write to impress your colleagues. 
          

          Warning: Notice in the last code snippet that std::vector<int> y(5) is very, very different from std::vector<int> y{5}

          [1]: https://isocpp.org/wiki/faq/cpp11-language#uniform-init

          Read and abide by the Qt Code of Conduct

          P 1 Reply Last reply 28 Apr 2024, 14:10
          5
          • K kshegunov
            28 Apr 2024, 10:27

            @Pl45m4 said in Initializer list syntax of subclass created with QtCreator class "wizard":

            Why that?

            The 'feature' (debatable how much of it is really a feature) is called uniform initialization. You may want to take a peek here [1].

            : QWidget(parent) is still valid, but should one continue to use {parent} from now on?

            Depends on your style and understanding. I don't, as I don't agree that I should mindlessly slap whatever the core guideline guidelines tells me to.

            Dis-/Advantages?!

            There are cases where that syntax has unexpected behaviour. If you have say:

            class Foo
            {
                Foo();
                Foo(std::initializer_list<Bar>);
            };
            

            You might run in trouble doing:

            Foo x{}; //< We call what exactly, is this the default constructor, or the one taking an initializer list?
            

            But I will grant you, the example is contrived. Here's a case where you may hit this in Qt.

            QJsonValue a{}; //< Do we mean to call QJsonValue(), or QJsonValue(QJsonObject(std::initializer_list<...>))
            

            As it happens, due to well C++, we call the constructors that take an initializer list in both cases. All in all we shot ourselves in the foot. So, if you ask me - use {} when you mean to use an initializer list, all other cases - simply call the constructor. That is to say:

            std::vector x = { 1, 2, 3, 4 }; //< Just fine!
            std::vector<int> y(5) //< Creating a vector that has 5 elements!!
            int q = 4; //< Can't get more explicit than that. `int q(4);` and `int q{4};` are both valid, but is it clearer to read? I sure don't think so. Looks more like something you'd write to impress your colleagues. 
            

            Warning: Notice in the last code snippet that std::vector<int> y(5) is very, very different from std::vector<int> y{5}

            [1]: https://isocpp.org/wiki/faq/cpp11-language#uniform-init

            P Online
            P Online
            Pl45m4
            wrote on 28 Apr 2024, 14:10 last edited by
            #4

            @kshegunov

            Thanks a lot :)


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            0
            • P Pl45m4 has marked this topic as solved on 28 Apr 2024, 14:10

            2/4

            27 Apr 2024, 18:13

            • Login

            • Login or register to search.
            2 out of 4
            • First post
              2/4
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved