【发布时间】:2013-04-01 11:17:27
【问题描述】:
我想专门化一个函数模板,以便返回类型根据模板参数的类型而变化。
class ReturnTypeSpecialization
{
public:
template<typename T>
T Item();
};
// Normally just return the template type
template<typename T>
T ReturnTypeSpecialization::Item() { ... }
// When a float is specified, return an int
// This doesn't work:
template<float>
int ReturnTypeSpecialization::Item() { ... }
这可能吗?我不能使用 C++11。
【问题讨论】:
-
你想达到什么目的?
-
我希望函数返回作为模板参数提供的类型,但在一种特殊情况下,我希望函数返回不同的类型。
-
仅作记录:如果模板参数是从参数的类型推导出来的,而不是明确指定的,完成返回不同类型的更简单方法是使用函数重载。 (当然这在这个例子中是行不通的,因为没有参数)