Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++_Templates_Explicit Instantiation - Fatal编程技术网

使用泛型类型显式实例化函数模板 我对C++模板没有太多的经验,所以我的术语可能会被取消。请容忍我,欢迎改正

使用泛型类型显式实例化函数模板 我对C++模板没有太多的经验,所以我的术语可能会被取消。请容忍我,欢迎改正,c++,templates,explicit-instantiation,C++,Templates,Explicit Instantiation,我有泛型类型已修复\u buf: 使用显式实例化,我还有以下几点: // in foo.cpp template string to_hex(const vector<unsign char>&); //在foo.cpp中 模板字符串到_十六进制(常量向量&); 如何使用fixed\u buf将显式实例化为_hex?是否可能?“显式实例化” 这意味着告诉编译器从具有某些指定类型的函数模板创建函数,即使它可能不需要(例如,能够链接到函数模板或减少编译时间) 模板可以看作是“

我有泛型类型
已修复\u buf

使用显式实例化,我还有以下几点:

// in foo.cpp
template string to_hex(const vector<unsign char>&);
//在foo.cpp中
模板字符串到_十六进制(常量向量&);
如何使用
fixed\u buf
显式实例化为_hex
?是否可能?

“显式实例化” 这意味着告诉编译器从具有某些指定类型的函数模板创建函数,即使它可能不需要(例如,能够链接到函数模板或减少编译时间)

模板可以看作是“类型级函数”。您的
to_hex
采用某种类型作为参数,并“返回”某种类型的函数

to_hex :: T -> to_hex<T>
您不能“传递”
fixed_buf
to
to_hex
;它不是一个类型,而是一个类型级函数。您只能传递
fixed\u buf
的结果。如果您不知道要传递给
fixed\u buf
的整数是什么(类型级别),则需要将其转换为(类型级别)函数:

\N -》 to_hex(fixed_buf(N)) :: int(N) -> to_hex<fixed_buf<N>>
这是一个过载(不是部分专业化;函数模板不允许这样做),应该可以工作


为什么首先需要显式实例化?为什么不像往常一样将实现移动到头文件?@tttapa只是为了分隔头文件。但这不是一个硬约束。但我不知道显式实例化不那么常见。
to_hex :: T -> to_hex<T>
fixed_buf :: int(N) -> fixed_buf<N>
\N -》 to_hex(fixed_buf(N)) :: int(N) -> to_hex<fixed_buf<N>>
template <int N>
string to_hex(const fixed_buf<N>& b) { /* implementation */ }