C++11 可变参数模板函数 -- 错误在哪里?

C++11 Variadic Template Function -- where's the error?

本文关键字:错误 在哪里 函数 变参 参数 C++11      更新时间:2023-10-16

下面的代码给出了msvc++ 2012 CTP(支持c++ 11)和Intel c++ XE 13.0的编译错误:

template <typename F, typename... Args>
    void apply(F f, std::tuple<Args...>& args) {
       // doesn't do much yet
}
bool f1(char c) {
    return c == 'c';
}
int main(int argc, char* argv[]) {
    auto t = std::make_tuple('c');
    apply(f1, t);
return 0;
}

VS2012错误是:

error C2243: 'type cast' : 
conversion from 'std::tuple<char,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil> *'
 to 'std::tuple<std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil> &' exists,
 but is inaccessible

Intel c++ XE 13.0错误:

error : no instance of function template "apply" matches the argument list

我错过了什么?这里真的有错误吗,还是我有两个不好的编译器?

UPDATE:当我在两个编译器上使用boost::tuple而不是std::tuple时,结果相同(或类似)。

附录:感谢所有评论中的交叉检查。我已经向这两家公司发送了bug报告。

这很可能是标准库实现中的VC11错误。尽管CTP支持可变模板,但据我所知,标准库并没有为了使用它们而重写,而是采用了一些机制来模拟可变模板。