c++ 11变量参数对齐



这是我想要实现的界面:

 Statement  select("SELECT * FROM People WHERE ID > ? AND ID < ?");
 select.execute(1462, 1477, [](int ID, std::string const& person, double item1, float item2){
     std::cout << "Got Row:" 
               << ID     << ", " 
               << person << ", " 
               << item1  << ", " 
               << item2  << "n";
 });

'?'在运行时与变量参数列表1462, 1477进行匹配。

这是类定义:

class Statement
{
    public:
        Statement(std::string const&);
        template<class Action, class ...Args>
        void execute(Args... param, Action action);
};

不幸的是,这会产生一个错误:

test.cpp:133:12:错误:调用execute时没有匹配的成员函数
选择。execute(1462, 1477, []) (int ID, std::string &Person, double item1, float item2){
~~~~~^~~~~~~

test.cpp:86:14:注意:候选模板被忽略:无法推断模板参数'Action'
无效执行(Args…参数,动作)
~~~^~~~~~~

产生1个错误。

但是如果我稍微改变函数定义(如下所示),它可以很好地编译。

class Statement
{
    public:
        Statement(std::string const&);
        template<class Action, class ...Args>
        void execute(Action action, Args... param);
                  // ^^^^^^^ Move action to the front.
};
// Also changed the call by moving the lambda to the first argument.

我知道这是一些语法糖变量参数列表的位置,但我想把变量参数列表放在第一位。我可以使用任何技巧来帮助编译器正确推断var参数列表吗?

这有点难看,但您可以使用元组:

#include <iostream>
#include <string>
#include <tuple>
template<int... Is>
struct integer_sequence {};
template<int N, int... Is>
struct make_integer_sequence : make_integer_sequence<N-1, N-1, Is...> {};
template<int... Is>
struct make_integer_sequence<0, Is...> : integer_sequence<Is...> {};
class Statement
{
    private:
        std::string foo;
    public:
        Statement(std::string const& p)
            : foo(p)
        {}
        template<class ...Args>
        void execute(Args... param)
        {
            execute_impl(make_integer_sequence<sizeof...(Args)-1>{}, param...);
        }
        template<int... Is, class... Args>
        void execute_impl(integer_sequence<Is...>, Args... param)
        {
            std::get<sizeof...(Args)-1>(std::tie(param...))
                (std::get<Is>(std::tie(param...))..., foo);
        }
};

使用例子:

int main()
{
    Statement s("world");
    s.execute("hello", ", ",
              [](std::string const& p1, std::string const& p2,
                 std::string const& p3)
              { std::cout << p1 << p2 << p3; });
    std::cout << "nENDn";
}

这里有一个替代的解决方案,不那么难看,但更冗长:

#include <iostream>
#include <string>
#include <tuple>
template<class Tuple, class...>
struct pop_back;
template<class T, class... Ts, class... Us>
struct pop_back<std::tuple<T, Ts...>, Us...>
    : pop_back<std::tuple<Ts...>, Us..., T>
{};
template<class T, class... Us>
struct pop_back<std::tuple<T>, Us...>
{
    using type = std::tuple<Us...>;
};
class Statement
{
    private:
        std::string foo;
    public:
        Statement(std::string const& p)
            : foo(p)
        {}
        template<class ...Args>
        void execute(Args... param)
        {
            helper<typename pop_back<std::tuple<Args...>>::type>
                ::execute(param..., foo);
        }
        template<class T>
        struct helper;
        template<class... Args>
        struct helper< std::tuple<Args...> >
        {
            template<class Action>
            static void execute(Args... param, Action action, std::string foo)
            {
                action(param..., foo);
            }
        };
};

这是一个简短的is_callable特性,允许static_assert提供更好的错误消息:

template<class F, class... Args>
struct is_callable
{
    template<class F1>
    static auto test(int)
        -> decltype( std::declval<F1>() (std::declval<Args>()...),
                     std::true_type{} );
    template<class F1>
    static std::false_type test(...);
    constexpr static auto value = decltype(test<F>(0))::value;
};
例如:

template<int... Is, class... Args>
void execute_impl(integer_sequence<Is...>, Args... param)
{
    auto& action = std::get<sizeof...(Args)-1>(std::tie(param...));
    auto param_tuple = std::tie(param...);
    static_assert(is_callable<decltype(action),
                              typename std::tuple_element<Is,
                                              decltype(param_tuple)>::type...,
                              decltype(foo)>::value,
                  "The action is not callable with those argument types.");
    action(std::get<Is>(param_tuple)..., foo);
}

如果参数包不在函数调用的末尾,则无法推断。

引自标准14.8.2.1 Deducing template arguments from a function call

末尾参数包的规则:

函数参数包出现在的每个剩余参数的类型A的声明符id的P类型对调用进行比较函数参数包。每次比较都会推导出模板参数对于模板参数包中由展开的后续位置函数参数包。

其他:

对于函数参数包,该参数包不出现在参数声明列表中,参数包的类型为non-deduced上下文。

最后我决定使用std::tuple作为参数并隐藏参数扩展:

用法:

select.execute(
    Bind(1462, 1477), 
    [](int ID, std::string const& person, double item1, float item2){
        std::cout << "Got Row:" 
                  << ID     << ", " 
                  << person << ", " 
                  << item1  << ", " 
                  << item2  << "n";
});

然后我定义了:

template<typename ...Args>
inline std::tuple<Args...> Bind(Args... args) { return std::tuple<Args...>(args...);}

类被重新定义为:

class Statement
{
    public:
        Statement(std::string const&);
        template<class Action, class ...Args>
        void execute(std::tuple<Args...> const& param, Action action);
};

最新更新