Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为函数调用指定嵌套模板参数_C++_C++11_Boost - Fatal编程技术网

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你应该把它作为一个新问题发布。