6

我正在尝试围绕用 C 编写的 SQL 库实现 C++11 包装器。C 库具有单独的函数,用于从需要列索引的 SQL 语句中获取不同的数据类型。下面是一个简单的方法原型,但有一个严重的缺陷:它依赖于参数执行的顺序,这是不安全的(也可能有编译器错误,没有测试过)。

问题:在可变参数模板扩展中安全地递增变量的独立于平台的方法是什么?

template< typename... ColumnTypes >
void SQLStatement::execute( std::function< void( ColumnTypes... ) > rowCallback ){
    while( this->nextRow() ){
        int column = 0;
        rowCallback( this->getColumn< ColumnTypes >( column++ )... );
        //                            unreliable increment ^
    }
}

template< typename T >
T SQLStatement::getColumn( const int columnIdx ){}

template<>
inline int SQLStatement::getColumn< int >( const int columnIdx ){
    return sql_library_column_int( this->nativeHandle, columnIdx );
}

// Other getColumn specializations here...
4

2 回答 2

5

这似乎有效。你只需要适应几件事:

#include <functional>
#include <iostream>
#include <cstddef>

void foo(int a, float b, int c) {
    std::cout << a << ", " << b << ", " << c << std::endl;
}

template<typename T>
T getColumn(int index) {
    return T(index);
}

template<size_t... indexes>
struct index_tuple {};

template<size_t head, size_t... indexes>
struct index_tuple<head, indexes...> {
    typedef typename index_tuple<head-1, head-1, indexes...>::type type;
};

template<size_t... indexes>
struct index_tuple<0, indexes...> {
    typedef index_tuple<indexes...> type;
};

template<typename... Args>
struct make_index_tuple {
    typedef typename index_tuple<sizeof...(Args)>::type type;
};

template<typename... ColumnTypes, size_t... indexes>
void execute(const std::function<void(ColumnTypes...)> &callback, index_tuple<indexes...>) {
    // this should be done for every row in your query result
    callback(getColumn<ColumnTypes>(indexes)...);
}

template<typename... ColumnTypes>
void execute(const std::function<void(ColumnTypes...)> &callback) {
    execute(
        callback, 
        typename make_index_tuple<ColumnTypes...>::type()
    );
}

int main() {
    std::function<void(int, float, int)> fun(foo);
    execute(fun);
}

演示在这里。请注意,该函数foo仅用于显示索引是否正确递增,就像return T(index);in一样getColumn

于 2013-04-02T04:21:03.117 回答
4

虽然 mfontanini 的解决方案有效并且很好,因为它在编译时执行递增列索引的计算,但我认为值得指出的是,对于如何在可变参数包扩展中递增 int 的问题也有直接答案。(不幸的是,由于错误,它似乎不适用于 GCC,请参阅最后的警告。)

答案基于这样一个事实:虽然函数调用中参数的评估是无序的,但列表初始化中的参数评估不是:

(第 8.5.4/4 节)在花括号初始化列表的初始化列表中,初始化子句,包括由包扩展(14.5.3)产生的任何初始化子句,按照它们出现的顺序进行评估。也就是说,与给定初始化子句相关联的每个值计算和副作用在初始化器列表的逗号分隔列表中与任何初始化子句相关联的每个值计算和副作用之前进行排序。
[注意:无论初始化的语义如何,这种评估顺序都成立;例如,它适用于初始化器列表的元素被解释为构造函数调用的参数时,即使调用的参数通常没有顺序约束。——尾注]

因此,如果您将函数调用转换为基于大括号初始化列表的内容,您将获得所需的效果:

rowCallback(std::tuple<ColumnTypes...> { getColumn<ColumnTypes>(column++)... });

这初始化了一个std::tuple使用列表初始化(注意大括号{ ... }),因此column++副作用将以从左到右的顺序执行。

如果按上述方式编写,这意味着您需要进行更改rowCallback()以使其接受 astd::tuple而不是参数列表。如果你不喜欢这样,你可以创建一个单独的模板函数,它调用扩展元组产生的参数上的call_on_tuple(fun,tup)任何函数。我曾经在这里描述了如何做到这一点,或者如果你喜欢,你可以从我的 GitHub 存储库中使用。funtuprlxutil::call_on_tuple

你的execute函数看起来像这样:

template <typename... ColumnTypes>
void execute(function<void(ColumnTypes...)> rowCallback)
{
  using std::tuple;
  using rlxutil::call_on_tuple;

  int column = 0;
  call_on_tuple(rowCallback,
                tuple<ColumnTypes...> { getColumn<ColumnTypes>(column++)... });
}

警告:这在 GCC 中无法按预期工作。我相信这是因为这里报告的错误:http: //gcc.gnu.org/bugzilla/show_bug.cgi?id= 51253

于 2013-04-02T04:40:53.353 回答