[英]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
for Future
和Incrementable
。
即將推出的C ++ Concepts技術規范的任何功能是否會放寬這些限制並允許調用具有多個參數包的函數模板?
Concepts Lite的約束系統位於現有模板機器之上。 特別是,它不會干擾模板參數推斷。 在你的例子中, Futures
包是不可扣除的,即使有概念也會如此。
然而,似乎兩個參數包原則上可以消除歧義,給出適當的Future for 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,您也可以輕松提供多個參數包,無需等待概念。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.