Overview
Features
Download
Documentation
Community
Add-Ons & Services

Poco::Data::Statement design error?

A general discussion forum.
Cyfer
Posts: 1
Joined: 15 Apr 2012, 02:25

Poco::Data::Statement design error?

Postby Cyfer » 15 Apr 2012, 02:52

The problem lies in the constructor of Poco::Data::Statement:

Poco/Data/Statement.h

Code: Select all

class Data_API Statement
{
public:
   explicit Statement(Session& session);


Why is it explicit?!
This prevents something like this:

Code: Select all

#include <Poco/Data/Statement.h>
#include <Poco/Data/Session.h>

Poco::Data::Session createSession() {
        return Poco::Data::Session("sqlite", "test.db");
}

/*
This is the ugly workaround due the needless explicit

int main() {
        Poco::Data::Session session(createSession()); // needless temporary object
        Poco::Data::Statement statement(session);
}
*/
/*
This does not work, because the constructor prevents it

int main() {
    Poco::Data::Statement statement(createSession()); // why should something like this not work?
}
*/


I know the example is totally pointless, but my scenario would be too complex.

It would be great if somebody could tell me if the problem lies in POCO or in my code.

alex
Posts: 1212
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Poco::Data::Statement design error?

Postby alex » 28 Apr 2012, 22:11

Cyfer wrote:The problem lies in the constructor of Poco::Data::Statement:

Code: Select all

Poco::Data::Statement statement(createSession()); // why should something like this not work?


Because
ISO/IEC 14882, 12.2.3 wrote:Temporary objects are destroyed as the last step in evaluating the full expression (1.9) that (lexically) contains the point where they were created.

Although you return a copy from createSession(), the Statement constructor takes a reference to it (and passes that reference to StatementImpl). Once the above statement is done, the original session you created does not exist anymore.


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest