STL style code.



  • 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);@



  • 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.



  • 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;@



  • 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;@



  • 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.



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.