C++ 为函数调用指定嵌套模板参数
如何在调用模板函数时为其指定类型C++ 为函数调用指定嵌套模板参数,c++,c++11,boost,C++,C++11,Boost,如何在调用模板函数时为其指定类型 #include <string> #include <utility> #include <boost/variant.hpp> template <typename O, typename E, template <typename, typename> class VariantType> VariantType<O, E> f(O o) { return VariantTy
#include <string>
#include <utility>
#include <boost/variant.hpp>
template <typename O, typename E, template <typename, typename> class VariantType>
VariantType<O, E> f(O o)
{
return VariantType<O, E>{std::move(o)};
}
int main()
{
boost::variant<int, std::string> res =
f<int, std::string, boost::variant<int, std::string>>(17);
}
你没有。将模板模板参数
模板类变量type
声明为f
的第三个模板参数意味着f
的第三个模板参数应该是类模板<代码>boost::variant是一个模板<代码>增强::variant不再是一个模板——它是一个模板的特殊化。你没有。将模板模板参数模板类变量type
声明为f
的第三个模板参数意味着f
的第三个模板参数应该是类模板<代码>boost::variant是一个模板<代码>boost::variant不再是一个模板——它是一个模板的特殊化。boost::variant模板类模板参数列表的arity不是两个。因此,通过将template
更改为template
来修复它:
#include <string>
#include <utility>
#include <boost/variant.hpp>
template <typename O, typename E, template <typename...> class VariantType>
VariantType<O, E> f(O o)
{
return VariantType<O, E>{std::move(o)};
}
int main()
{
boost::variant<int, std::string> res = f<int, std::string, boost::variant>(17);
}
#包括
#包括
#包括
样板
变量类型f(O)
{
返回VariantType{std::move(o)};
}
int main()
{
boost::variant res=f(17);
}
boost::variant template类模板参数列表的arity不是两个。因此,通过将template
更改为template
来修复它:
#include <string>
#include <utility>
#include <boost/variant.hpp>
template <typename O, typename E, template <typename...> class VariantType>
VariantType<O, E> f(O o)
{
return VariantType<O, E>{std::move(o)};
}
int main()
{
boost::variant<int, std::string> res = f<int, std::string, boost::variant>(17);
}
#包括
#包括
#包括
样板
变量类型f(O)
{
返回VariantType{std::move(o)};
}
int main()
{
boost::variant res=f(17);
}
所以不可能调用像f
这样的函数?很抱歉,但是f
也不起作用。你能演示一下如何修复我的代码吗?@user1244932你应该把它作为一个新问题发布。因此不可能调用像f
这样的函数。很抱歉,f
也不起作用。你能展示一下如何修复我的代码吗?@user1244932你应该把它作为一个新问题发布。