c++ Concepts TS会启用多个参数包吗?
Will the C++ Concepts TS enable multiple parameter packs?
在c++ 14中,不可能调用带有多个参数包的函数模板:
#include <future>
template<class... Futures, class... Incrementables>
void foo(Futures&... futures, Incrementables... incrementables)
{
}
int main()
{
std::future<int> a, b;
int x, y;
// ERROR
foo(a, b, x, y);
return 0;
}
因为不清楚第一个参数包在哪里结束,第二个参数包在哪里开始,所以如果没有程序员提供的额外信息,就不可能调用foo
。
然而,似乎这两个参数包在原则上可以消除歧义,给Future
和Incrementable
适当的概念。
即将发布的c++概念技术规范是否会放宽这些限制,允许调用具有多个参数包的函数模板?
Concepts Lite的约束系统位于现有模板机制之上。特别是,它不会干扰模板参数的推导。在您的示例中,Futures
包是不可演绎的,并且即使使用概念也将保持如此。
然而,似乎这两个参数包在原则上可以消除歧义,给予适当的Future和Incrementable概念。
你可能没有选到最好的例子,尽管这并不会让你的问题的前提变得不那么有趣。你怎么看这个?
Future{Fut}
struct incrementable_future: Fut {
using Fut::Fut;
incrementable_future& operator++() { return *this; }
};
在c++ 14中,不可能调用包含多个参数包的函数模板
正如在对这个问题的评论中提到的,只要这些包可以推导出来,即使在c++ 14中也是可能的。
它遵循一个最小的工作示例:
#include <tuple>
#include<functional>
template<class... T, std::size_t... I, typename... U>
void foo(std::tuple<T...> ts, std::index_sequence<I...>, std::tuple<U...> us)
{ }
int main() {
foo(std::make_tuple(42, 'b'), std::make_index_sequence<10>(), std::make_tuple(0., 'c'));
return 0;
}
另一种方法是通过模板特化:
template<typename, typename>
struct S;
template<typename R1, typename... A1, typename R2, typename... A2>
struct S <R1(A1...), R2(A2...)> {};
int main() {
S<void(int, char), int(char, float, double)> s;
}
这里是第三个不需要std::tuple
或其他工件的例子:
template<typename... A, typename... B>
void f(B...) { }
int main() {
f<int, char>(42, 0.);
}
这里的技巧在于参数包A
是在函数调用时显式指定的,而参数包B
是从函数实参中推导出来的。
这表明即使使用c++ 11,您也可以轻松地提供多个参数包,而无需等待概念。
相关文章:
- 如何反转整数参数包
- 如何将enable-if与模板参数和参数包一起使用
- 模板元编程:如何将参数包组合成新的参数包
- C ++:在构造函数中使用参数包?
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 双模板参数包
- 参数包构造函数在类模板中隐藏用户定义的转换
- 嵌套参数包扩展失败
- 参数包中的筛选器类型
- 参数和参数包的类型推导
- 为模板参数包添加别名
- enable_if中参数包的大小问题
- 如何使特征接受参数包?
- C++:我可以在模板参数包中强制执行至少1个agment吗
- 有什么优雅的方式吗?(类型参数包)
- 我可以有常量参数包吗
- 我可以展开一个参数包并定义一个参数列表吗?
- c++ Concepts TS会启用多个参数包吗?
- 计算结果为"void..."的非类型参数包不是非法的吗?
- 可以在c++ 11 lambda中隐式捕获参数包吗?