Edgard

讨论FunctionTemplate申明的隐藏性(Visibility)

看看两端代码,区别重要在:
一个是:inline int const& max申明在template <typename T>
inline T const& max之前。


// maximum of two int values
inline int const& max (int const& a, int const& b)
{
    return a<b?b:a;
}

// maximum of two values of any type
template <typename T>
inline T const& max (T const& a, T const& b)
{
    return a<b?b:a;
}

// maximum of three values of any type
template <typename T>
inline T const& max (T const& a, T const& b, T const& c)
{
    return max (max(a,b), c); 
}                              

一个是:inline int const& max申明在template <typename T>
inline T const& max之后。

// maximum of two values of any type
template <typename T>
inline T const& max (T const& a, T const& b)
{
    return a<b?b:a;
}

// maximum of three values of any type
template <typename T>
inline T const& max (T const& a, T const& b, T const& c)
{
    return max (max(a,b), c); 
}

// maximum of two int values
inline int const& max (int const& a, int const& b)
{
    return a<b?b:a;
}

调用程序:
int main( )
{
      //当然这里本来就写得不好,要先显式申明写局部变量......
      // 看你了解多少,讨论讨论两种执行可能的执行路径,即:FunctionTemplate的调用路径!!!
      ::max( 4, 10 ,15 );


posted on 2005-12-15 21:10 Edgard 阅读(484) 评论(5)  编辑 收藏 引用

评论

# re: 讨论FunctionTemplate申明的隐藏性(Visibility) 2005-12-16 10:26 小明

Function templates can be overloaded with nontemplate functions. All else being equal, the nontemplate function is preferred in Function templates can be overloaded with nontemplate functions. All else being equal, the nontemplate function is preferred in selecting the actual function being called. The following example illustrates this:

// details/nontmpl.cpp

#include <string>
#include <iostream>

template<typename T>
std::string f(T)
{
return "Template";
}

std::string f(int&)
{
return "Nontemplate";
}

int main()
{
int x = 7;
std::cout << f(x) << std::endl;
}
This should output:

Nontemplate


以上是C++ templates的原文。
所以nontemplate的函数应该被匹配调用,跟声明的前后没关系

  回复  更多评论   

# re: 讨论FunctionTemplate申明的隐藏性(Visibility) 2005-12-16 12:47 Edgard

上面是我读一本书测试过的例子,书中曾说:This is only one example of code that might behave differently than expected as a result of detailed overload resolution rules. For example, the fact that not all overloaded functions are visible when a corresponding function call is made may or may not matter. In fact, defining a three-argument version of max() without having seen the declaration of a special two-argument version of max() for ints causes the two-argument template to be used by the three-argument version:,这里的意思比较适合第二种情况,即”一个是:inline int const& max申明在template <typename T>
inline T const& max之后。”,我没有能在其他C++编译器上测试过,不知道C++标准中是否规定FunctionTemplate Resolution Rules,如果没有,依据上面的英文描述,不同编译器有不同的FunctionTemplate Resolution Rule,有可能template <typename T>
inline T const& max (T const& a, T const& b, T const& c) 不可见到申明在后的inline int const& max。

大家的意见呢?  回复  更多评论   

# re: 讨论FunctionTemplate申明的隐藏性(Visibility) 2005-12-17 15:08 清风雨

无意闯到这边来了。

我是在对几个概念不太清楚的情况下,想找找说明。本来是不管这些概念的,但,我想试试特化是不小心写错了,编译器告诉我无法显示化。
我就纳闷了,特化、偏特化、显示化,所以,想找找概念的介绍,一不小心发现了这个blog站,而你说的问题,我又忍不住想开一下口。^_^

关于特化的匹配原则,就像我们期望的那样,编译器是优选最接近的。
最简单的测试方法,就是针对模板圆形,特化形式分别给出不同输出,这样,一眼便能知道编译器选了谁。  回复  更多评论   

# re: 讨论FunctionTemplate申明的隐藏性(Visibility) 2006-01-08 20:27 chenchen

<c++ template>上说必须把重载的写在template的前面,然而我在vs2003上的测试是无所谓,至于标准上怎么说那我就不知道了,写在前面就一定保险,写在后面估计是有危险!  回复  更多评论   

# re: 讨论FunctionTemplate申明的隐藏性(Visibility) 2007-05-27 23:37 xiongx

这玩意跟C的先申明后使用是一脉相承的吧,template的很多编译策略都是看起来能用就行,不会顾及全面的  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理