Pra*_*han 3 templates boost c++11
我在使用boost :: tuple部分专门化模板时遇到了错误.使用std :: tuple替换boost :: tuple时编译的代码相同.这是压缩到无法编译的部分的代码.
template <typename... Args>
class Test;
template <typename... Args>
class Test<std::tuple<Args...>, Args...>
{
};
template <typename... Args>
class Test<boost::tuple<Args...>, Args...>
{
};
int main()
{
int rc;
cout<<abi::__cxa_demangle(typeid(Test<boost::tuple<int, int>, int,int>).name(), 0, 0, &rc)<<endl;//Doesn't compile
cout<<abi::__cxa_demangle(typeid(Test<std::tuple<int, int>, int,int>).name(), 0, 0, &rc)<<endl;//Compiles
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译错误,使用g ++ 48,是
tuplerr.cpp: In function ‘int main()’:
tuplerr.cpp:30:73: error: invalid use of incomplete type ‘class Test<boost::tuples::tuple<int, int, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>, int, int>’
cout<<abi::__cxa_demangle(typeid(Test<boost::tuple<int, int>, int,int>).name(), 0, 0, &rc)<<endl;//Doesn't compile
^
tuplerr.cpp:14:7: error: declaration of ‘class Test<boost::tuples::tuple<int, int, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>, int, int>’
class Test;
Run Code Online (Sandbox Code Playgroud)
但是,使用std :: tuple的专业化工作得很好.我究竟做错了什么?
试试这个
// used below to create a "non-deduced context"
template<typename T>
struct Id { typedef T type; };
template <typename... Args>
class Test;
template <typename... Args>
class Test<typename Id<std::tuple<Args...>>::type, Args...>
{
};
template <typename... Args>
class Test<typename Id<boost::tuple<Args...>>::type, Args...>
{
};
Run Code Online (Sandbox Code Playgroud)
因此,可选参数boost::tuple
(由于你的boost的元组实现使用它来模拟可变参数模板而具有默认参数)不会使第一个Args...
与Args...
仅具有所需显式传递参数的扩展相矛盾.