Skip to content

Is it better to use T{} instead of T() in Statement::getColumns()? #130

@cycycyc

Description

@cycycyc

In the helper function called by getColumns<typename T, int N>, the returned T is constructed by T():

    // Helper function called by getColums<typename T, int N>
    template<typename T, const int... Is>
    T Statement::getColumns(const std::integer_sequence<int, Is...>)
    {
        return T(Column(mStmtPtr, Is)...);
    }

If I pass a struct without a constructor like:

    struct Test { int a; double b; const char* c; };
    auto test = query.getColumns<Test, 3>();

There will be a error:
error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Statement_getColumns_Test::TestBody::Test'
I believe the initializer list, that is, the expanded parameter pack, will be first cast to Test, and then the move constructor will be invoked.

If I change T() to T{} like:

    // Helper function called by getColums<typename T, int N>
    template<typename T, const int... Is>
    T Statement::getColumns(const std::integer_sequence<int, Is...>)
    {
        return T{Column(mStmtPtr, Is)...};
    }

The error will be gone. The initializer list will be directly passed to construct the struct Test.

So I think it would be easier to use without defining a constructor for each struct when using getColumns(), by changing T() to T{}.

Please correct me if I'm wrong. Thanks.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions