Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. STL style code.
QtWS25 Last Chance

STL style code.

Scheduled Pinned Locked Moved The Lounge
6 Posts 2 Posters 4.0k 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.
  • X Offline
    X Offline
    xeonn
    wrote on last edited by
    #1

    Would be nice to have..
    @
    QWidget * newWidget = new QWidget();

    // Setting title/data
    newWidget << QVariant("new data");

    // Retrieve data from widget
    QVariant data;
    newWidget >> data;

    // Setting background color to black
    newWidget << QWidget::Properties::background << QBrush(black);@

    1 Reply Last reply
    0
    • I Offline
      I Offline
      ivan
      wrote on last edited by
      #2

      I don't like it - for me it is a misuse of << and >>.

      For example, << and >> operators are usually made to be chainable, what would the following do?

      @newWidget << QVariant("new data") << QVariant("new data 2");@

      p.s. Your last example looks a lot like the declarative way of programming (only a lot longer to type), and that is already available via QML.

      Ivan Čukić | ivan.cukic(at)kde.org | KDE e.V.
      Qt Ambassador (from the old Nokia days)

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xeonn
        wrote on last edited by
        #3

        Ya. It should be chainable. And they are just syntactic sugar for STL fan like me.

        The meaning is up to Qt developer to decide. They can be anything. Different module can bring different meaning. Not a problem. Example from SOCI, database library:-
        @
        session sql;
        sql.open(postgresql, "dbname=mydb");
        sql << "drop table person";
        sql << "select id, name from company";

        // select operation
        row r;
        string name, address;
        int age;
        r >> name >> address >> age;
        @

        @From OTL database library
        int nID; string name; string job_desc;
        otl_stream is(...connection and sql stmt...);
        is >> nID >> name >> job_desc; // position defines what field/column@

        Then the library can use STL algorithm function like 'copy' or input or output iterator
        @rowset<string> rs = (sql.prepare << "select firstname from person");

        std::copy(rs.begin(), rs.end(), std::ostream_iteratorstd::string(std::cout, "\n"));@

        The notion brings back nostalgia for CS student when they first started to learn c++.
        @cout << "Hello world" << endl;@

        1 Reply Last reply
        0
        • I Offline
          I Offline
          ivan
          wrote on last edited by
          #4

          In all real-world examples you've mentioned, those are stream-like structures. And those do have << and >> operators defined in Qt.

          The problem (from my point of view) with having the operators in question do fancy stuff like you're proposing is that in STL they are not fancy at all. For example, it would be like introducing into std::map something like this:

          @std::map < std::string, int > m;
          m << std::map::key("asd") << 5;@

          Ivan Čukić | ivan.cukic(at)kde.org | KDE e.V.
          Qt Ambassador (from the old Nokia days)

          1 Reply Last reply
          0
          • X Offline
            X Offline
            xeonn
            wrote on last edited by
            #5

            The actual syntax for std::map is

            @std::map<std::string, int> m;
            m["asd"] = 5;@

            to shorten, we drop 'std::'
            @using namespace std;
            map<string,int> m;
            m["asd"] = 5;@

            The std library tries at best to match natural syntax.
            In database programming, it tries to match SQL dialect. http://soci.sourceforge.net/doc/index.html
            In parser/lexer libraries, it tries to match EBNF-style natural. http://www.boost.org/doc/libs/1_44_0/libs/spirit/doc/html/spirit/preface.html

            Lambda notation and so many others. No doubt lots of abuse on operator overloading.

            1 Reply Last reply
            0
            • I Offline
              I Offline
              ivan
              wrote on last edited by
              #6

              I know the syntax of std::map, I'm just trying to make an analogy for your desire to convert

              @widget->setBackground(QBrush(black));@

              into

              @newWidget << QWidget::Properties::background << QBrush(black);@

              As for functional programming and lambda calculus, see the forthcoming version of C++, currently known as C++0x

              Ivan Čukić | ivan.cukic(at)kde.org | KDE e.V.
              Qt Ambassador (from the old Nokia days)

              1 Reply Last reply
              0

              • Login

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