STL style code.
-
wrote on 7 Sept 2010, 17:58 last edited by
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);@ -
wrote on 7 Sept 2010, 19:46 last edited by
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.
-
wrote on 7 Sept 2010, 22:05 last edited by
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;@ -
wrote on 7 Sept 2010, 22:21 last edited by
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;@ -
wrote on 7 Sept 2010, 22:39 last edited by
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.htmlLambda notation and so many others. No doubt lots of abuse on operator overloading.
-
wrote on 8 Sept 2010, 07:19 last edited by
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
6/6