作为一个大型项目的一部分,我正在使用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;
}
根据这个答案,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
^
我能在这种情况下使用大括号语法吗?
一些背景:这将用于重载运算符,所以我想我只能使用元组,并且不能使用可变参数,任何提示都可以接受。
额外信息: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) {}