首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模板:函数模板专门化:模板参数演绎:-->有人能为这条语句说出更多的例子吗?

模板:函数模板专门化:模板参数演绎:-->有人能为这条语句说出更多的例子吗?
EN

Stack Overflow用户
提问于 2010-10-22 19:36:21
回答 2查看 382关注 0票数 0

这是国际标准化组织C++标准14.8.2.4 /3:从类型推导模板参数中的语句

代码语言:javascript
复制
  A given type P can be composed from a number of other types, templates, 
  and non-type values:

    — A function type includes the types of each of the function parameters 
      and the return type.
    — A pointer to member type includes the type of the class object pointed 
      to and the type of the member pointed to.
    — A type that is a specialization of a class template (e.g., A<int>) 
      includes the types, templates, and non-type values referenced by the  
      template argument list of the specialization.
    — An array type includes the array element type and the value of the  
      array bound.

               In most cases, the types, templates, and non-type values that 
 are used to compose P participate in template argument deduction. That is,they
 may be used to determine the value of a template argument, and the value so 
 determined must be consistent with the values determined elsewhere. In certain  
 contexts, how ever, the value does not participate in type deduction, but 
 instead uses the values of template arguments that  were either deduced 
 elsewhere or explicitly specified. If a template parameter is used only in 
 nondeduced contexts and is not explicitly specified, template argument 
 deduction fails.

总之,我尝试用函数模板的地址来推导模板参数,以及推导转换函数模板参数。

在第4点,..i知道编译器不能推导出主数组绑定的值,除非绑定引用或指针类型。主数组边界不是函数参数类型的一部分。

谁能用一个例子来解释每一点…

EN

回答 2

Stack Overflow用户

发布于 2010-10-22 19:47:55

不知道你在问什么,但是关于“编译器不能推导出一个主数组界限的值,除非这个界限引用一个引用或指针类型”,我认为里面一定有一些打字错误。

从包含bound的数组类型中推导模板参数的规范示例如下

代码语言:javascript
复制
typedef ptrdiff_t Size;    // For example.

template< class Type, Size n >
Size countOf( Type (&)[n] ) { return n; }

其中,Typen是从作为实际参数提供的数组中推导出来的

干杯&哈。

票数 1
EN

Stack Overflow用户

发布于 2010-10-22 21:11:11

下面是一些例子:

代码语言:javascript
复制
#include <string.h>
#include <ctype.h>
#include <utility>
#include <vector>

template <class R, class F, class S>
void function_pointer( R (*func)(F, S) );

template <class T, class Object>
void member_pointer( T Object::*);

template <class T, class U, template <class> class Alloc>
void a_vector(const std::vector<T, Alloc<U> >&);

template <class T, std::size_t N>
void array_ref(T (&)[N]);  //arrays decay to pointer when passed by value

template <class T, class U>
void types_must_agree(std::pair<T, U>, T (*) (U));

template <class T>
void non_deduced_context(typename std::pair<T, T>::first_type);

template <class T>
void non_deduced_context_deduced_from_another_argument(typename std::pair<T, T>::first_type, T);

int main()
{
    function_pointer(&strcmp);
    member_pointer(&std::pair<int, double>::first);
    a_vector(std::vector<int>());
    char c[10];
    array_ref(c);
    types_must_agree(std::pair<int, int>(), &isspace);
    non_deduced_context<int>(10);
    non_deduced_context_deduced_from_another_argument(10, 20);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3996388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档