【发布时间】:2015-09-28 17:37:51
【问题描述】:
我希望编写一个函数模板来查找作为参数传入的两个数字中较大的一个。我想为int显式实例化这个函数模板。
所以我在 large.h 中写了以下内容;
template <typename T>
const T& larger(T a,T b);
我在 large.cpp 中写了以下内容:
#include<iostream>
#include"larger.h"
const T& larger(T a,T b) {
return a<b?b:a;
}
在main.cpp中:
#include<iostream>
#include "larger.h"
int main(){
template int()
const& larger<int>(int,int);
std::cout << larger(6,8) << std::endl;
}
当我编译并尝试运行 main.cpp 时,我收到以下错误:
error: expected primary-expression before ‘template’
template int()
我无法继续进行。怎么了?
【问题讨论】:
-
std::max 有什么问题?它已经存在并且完全符合您的要求。
-
template int()应该是什么?
标签: c++ function templates instantiation explicit