【发布时间】:2018-04-25 20:54:00
【问题描述】:
作为更大项目的一部分,我正在使用std::tuple 和模板;考虑以下代码:
template <typename ...T> void foo(tuple<T...> t) {}
void bar(tuple<int, char> t) {}
tuple<int, char> quxx() { return {1, 'S'}; }
int main(int argc, char const *argv[])
{
foo({1, 'S'}); // error
foo(make_tuple(1, 'S')); // ok
bar({1, 'S'}); // ok
quxx(); // ok
return 0;
}
根据this answer C++17 支持从 copy-list-initialization 进行元组初始化,但是似乎这种支持是有限的,因为我收到以下错误(GCC 7.2.0):
main.cpp: In function 'int main(int, const char**)':
main.cpp:14:17: error: could not convert '{1, 'S'}' from '<brace-enclosed initializer list>' to 'std::tuple<>'
foo({1, 'S'}); // error
^
有什么方法可以在这种情况下使用大括号括起来的语法吗?
Some Context :这将在运算符重载中使用,所以我想我必须使用元组并且不能使用可变参数,任何提示都可以接受。
额外:Clang 6 也抱怨
prog.cc:12:5: error: no matching function for call to 'foo'
foo({1, 'S'}); // error
^~~
prog.cc:6:31: note: candidate function [with T = <>] not viable: cannot convert initializer list argument to 'tuple<>'
template <typename ...T> void foo(tuple<T...> t) {}
【问题讨论】:
标签: c++ variadic-templates c++17 initializer-list uniform-initialization