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"
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved C++ Gurus
initializerlistc standardparenthesesbrackets
4 Posts 3 Posters 1.2k Views 2 Watching
  • 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.
  • Pl45m4P Offline
    Pl45m4P Offline
    Pl45m4
    wrote on 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

    JonBJ kshegunovK 2 Replies Last reply
    0
    • Pl45m4P Pl45m4

      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 :)

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on 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

      Pl45m4P 1 Reply Last reply
      5
      • Pl45m4P Pl45m4

        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 :)

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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
        • Pl45m4P Pl45m4

          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 :)

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on 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

          Pl45m4P 1 Reply Last reply
          5
          • kshegunovK kshegunov

            @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

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on 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
            • Pl45m4P Pl45m4 has marked this topic as solved on

            • Login

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