功能中的参数打包后的参数 [英] Parameters after parameter pack in function

查看:48
本文介绍了功能中的参数打包后的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够找到关于一个问题似乎是在问与我相同或相似的问题,但没有答案:-(

I was able to find one question on SO that seems to be asking the same or similar question that I am, but there are no answers :-(

我想在参数包之后放置一个非模板参数.我对可变参数模板/参数包的C ++标准规范不太熟悉,但是我的常识假设告诉我,传递给函数的最右边的参数将首先填充到放置参数中,然后将其余参数填充到参数包.但是,我无法在g ++或clang ++上使用我的测试代码.下面的示例代码.

I'd like to place a non-template parameter after a parameter pack. I'm not too familiar on the C++ standard specification for variadic templates / parameter packs, but my common sense assumption tells me that the right-most parameters passed to a function would be filled into placement parameters first, then the rest get filled into the parameter pack. However, I'm not able to get my test code working on either g++ or clang++. Sample code below.

#include <vector>
#include <iostream>

int Subscribe(int channel, int callback)
{
    return channel;
}

// This one works fine...
template<typename... T>
std::vector<int> SubscribeMultiple1(int callback, T&&... channels)
{
    return {
        Subscribe(std::forward<T>(channels), std::move(callback))...
    };
}

// This one does not work; all I did was move `int callback` after the parameter pack.
template<typename... T>
std::vector<int> SubscribeMultiple2(T&&... channels, int callback)
{
    return {
        Subscribe(std::forward<T>(channels), std::move(callback))...
    };
}

int main()
{
    auto subs = SubscribeMultiple2(1, 2, 3);

    for (auto sub : subs)
    {
        std::cout << "Sub: " << sub << '\n';
    }
}

实时示例

所以我的第一个问题是,为什么非模板参数在参数打包后不起作用?我做错什么了吗?还是语言禁止这样做?

So my first question is, why does the non-template parameter not work after the parameter pack? Am I doing something wrong or is this prohibited by the language?

第二,有什么方法可以获取我要尝试的语法?我简化了示例,但是在我的真实代码中, callback 参数实际上是一个 std :: function< ...> .我的想法是我可以订阅许多事件ID",并在最后定义回调.在结尾加上回调会更好地提高可读性和样式.示例:

Secondly, is there any way to get the syntax I'm trying to do? I've oversimplified my sample, but in my real code the callback parameter is really a std::function<...>. The idea is I can subscribe to a number of "event IDs", and the callback is defined at the end. Having the callback at the end is better for readability and style. Example:

SubscribeMultiple(EventOne, EventTwo, EventThree, [] {
    // Implement callback code here when any of the above
    // three events are fired.
});

如果我必须在前面加上回调,恕我直言.因此,我愿意尝试任何变通方法以获得我想要的语法和结构.预先感谢.

If I have to have the callback in the front, it's less readable IMHO. So I'm willing to try any workaround to get the syntax and structure I'm aiming for. Thanks in advance.

推荐答案

我做错什么了吗?还是语言禁止这样做?

仅当参数包是最后一个参数时才进行参数包的推导.

Deduction of parameter packs occurs only when the pack is the last argument.

[temp.deduct.type]/5.7 :

非推论上下文是:

The non-deduced contexts are:

  • [...]
  • parameter-declaration-list 的末尾没有出现的功能参数包.
  • [...]
  • A function parameter pack that does not occur at the end of the parameter-declaration-list.

这是标准C ++的正常行为.

So that's normal behavior of standard C++.

有没有办法获取我要尝试的语法?

使用第一种语法作为解决方法:

Use the first syntax as a workaround:

   template<typename... T>
   std::vector<int> SubscribeMultiple1(int callback, T&&... channels)

这篇关于功能中的参数打包后的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆