模板函数调用

我在看boost的ASIO源代码的时候,发现一个没有见过的模板调用用法.google没有说明,baidu也没有说明,只好自己试一下。下面是ASIO调用的代码:在文件service_registry.hpp中。
 1   // Check whether a service object of the specified type already exists.
 2   template <typename Service>
 3   bool has_service() const
 4   {
 5     boost::asio::detail::mutex::scoped_lock lock(mutex_);
 6 
 7     boost::asio::io_service::service* service = first_service_;
 8     while (service)
 9     {
10       if (service_id_matches(*service, Service::id))
11         return true;
12       service = service->next_;
13     }
14 
15     return false;
16   }
调用的地方,红色标出的地方 
template <typename Service>
bool has_service(io_service& ios)
{
  
// Check that Service meets the necessary type requirements.
  (void)static_cast<io_service::service*>(static_cast<Service*>(0));
  (
void)static_cast<const io_service::id*>(&Service::id);

  
return ios.service_registry_->template has_service<Service>();
}

下面是测试例子,在VS2005中编译通过。原来,函数使用的时候,可以完全指定模板函数定义。
 1 class VV
 
2 {
 
3 public:
 
4     static int getI()
 
5     {
 
6         return 100;
 
7     }
 
8 };
 
9 class V
10 {
11 public:
12     template<class K>
13     void PrintK()
14     {
15         cout<<K::getI()<<endl;
16     }
17 };
18 void m()
19 {
20     V v;
21     v.template PrintK<VV>();
22 }



ANSI C++ '03
14.2 Names of template specializations
4 When the name of a member template specialization appears after . or -> in a postfix-expression, or after
nested-name-specifier in a qualified-id, and the postfix-expression or qualified-id explicitly depends on a
template-parameter (14.6.2), the member template name must be prefixed by the keyword template.
Otherwise the name is assumed to name a non-template. [Example:
class X {
public:
template<size_t> X* alloc();
template<size_t> static X* adjust();
};
template<class T> void f(T* p)
{
T* p1 = p->alloc<200>();
// ill-formed: < means less than
T* p2 = p->template alloc<200>();
// OK: < starts template argument list
T::adjust<100>();
// ill-formed: < means less than
T::template adjust<100>();
// OK: < starts template argument list
}

### 关于函数调用模板的示例代码 以下是基于C++中的函数调用模板的一个综合示例,展示了如何定义和使用函数模板以及其重载机制: ```cpp #include <iostream> using namespace std; // 定义一个通用的函数模板用于比较两个值的最大值 template<typename T> T getMax(T a, T b) { cout << "Template function called." << endl; return (a > b) ? a : b; } // 普通函数版本,优先级高于同名模板函数 double getMax(double a, double b) { cout << "Ordinary function called." << endl; return (a > b) ? a : b; } // 特殊化的模板函数,针对字符串类型 template<> const string& getMax<string>(const string& a, const string& b) { cout << "Specialized template function for strings called." << endl; return (a.length() > b.length()) ? a : b; } int main() { int x = 10, y = 20; cout << "Max of integers: " << getMax(x, y) << endl; // 调用模板函数 double m = 3.5, n = 4.7; cout << "Max of doubles: " << getMax(m, n) << endl; // 调用普通函数 string s1 = "hello", s2 = "world"; cout << "Max of strings by length: " << getMax(s1, s2) << endl; // 调用特殊化模板函数 char c1 = 'A', c2 = 'B'; cout << "Max of characters: " << getMax(c1, c2) << endl; // 调用模板函数 return 0; } ``` #### 解析 在这个例子中,`getMax<T>` 是一个泛型函数模板[^1]。当传入不同类型的参数时,编译器会自动推导并实例化相应的模板函数。 对于 `double` 类型的情况,由于存在显式的普通函数 `getMax(double, double)`,因此在这种情况下会优先调用普通函数而不是模板函数。 另外还展示了一个全特化模板的例子——专门针对 `std::string` 的最大长度比较逻辑进行了定制实现[^5]。 --- ### 进一步说明 在实际开发过程中,为了提高代码可读性和维护性,通常建议将模板定义放置在头文件中以便被多个源文件共享[^3]。此外需要注意的是,在某些复杂场景下可能还需要考虑偏特化或默认参数等问题。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值