对未显式实例化的类型使用模板函数?
我创建了以下内容来尝试更好地理解模板函数的显式实例化。我的印象是(现在看来是错误的),如果您显式实例化模板函数,那么您只能将模板函数用于显式实例化期间使用的类型,在本例中只是 double 。
但在这里,我将它用于 std::string 并编译代码。我的理解不正确吗?
#include <iostream>
#include <string>
template <typename dim>
void func(dim i) {
std::cout << i << std::endl;
}
template void func<double>(double i);
int main(int argc, char const *argv[])
{
func<std::string>("123");
}
I created the following to try to better understand explicit instantiation of template functions. I was under (what appears to be wrong now) the impression that if you explicitly instantiate a template function, then you can only use the template function for the types that were used during explicit instantiation, which in this case is just double
.
But here, I used it for std::string
and the code compiles. Is my understanding incorrect?
#include <iostream>
#include <string>
template <typename dim>
void func(dim i) {
std::cout << i << std::endl;
}
template void func<double>(double i);
int main(int argc, char const *argv[])
{
func<std::string>("123");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论