在C ++中函数/方法/模板名称解析的首选项是什么? [英] What is the preference of function/method/template name resolving in C++?

查看:215
本文介绍了在C ++中函数/方法/模板名称解析的首选项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有多种可能性,C ++编译器如何决定调用哪个函数/方法?
在我的具体情况下,我有C ++运行时间的标准自由功能,我还有一个模板的自由变量,像这样:

  // C ++运行时库的定义(从memory.h)
extern malloc(size_t s);
extern void free(void * p);

//我们自己的内存管理函数
extern void * OurMalloc(size_t s);
extern void OurFree(void * p);

//自己的变量来覆盖malloc和free(而不是使用#define)
template< typename T>
void * malloc(T t)
{
return OurMalloc(t);
}

template< typename T>
void free(T * t)
{
OurFree(t);
}

我使用下面的代码测试了这个:

  void main(void)
{
void * p = malloc
free(p);
}

如果我编译并运行这个,似乎malloc的调用是正确的替换为模板变体。到目前为止,这么好。



但是,对free的调用不会被模板变体所取代,而且标准C ++函数仍然被调用。



C ++编译器使用哪些规则来决定哪个变量具有优先级?
这是与Koenig查找规则相关吗?



注意:我试过这个替代,因为使用#define不能解决问题=http://stackoverflow.com/questions/2319381> http://stackoverflow.com/questions/2319381 )。

解决方案

过载分辨率一般很复杂。



在你的情况下,这很容易:如果有一个精确的匹配。对于自由是这样的情况(标准的free需要一个void *),对于malloc它不是(标准malloc需要一个size_t,你传递一个int和size_t不能是一个typedef int - size_t是无符号)。



正在运行:



<$ p如果您使用void *之外的类型调用free, $ p> #include< iostream>

void * ml(size_t s)
{
std :: cout< ml(size_t)\\\
;
}

void fr(void * p)
{
std :: cout< fr(void *)\\\
;
}

template< typename T>
void * ml(T t)
{
std :: cout< ml << typeid(T).name()<< (T)\ n;
}

template< typename T>
void fr(T * t)
{
std :: cout< fr << typeid(T).name()<< (T *)\\\
;
}

int main()
{
void * p1 = ml((size_t)10);
fr(p1);
int * p2 =(int *)ml(10);
fr(p2);
return 0;
}

我得到

  ml(size_t)
fr(void *)
ml< i>

且i是返回 typeid(int).name()


How does the C++ compiler decide which function/method to call if there are multiple possibilities? In my specific case I have the standard free function of the C++ Run time and I also have a templated free variant, like this:

// The definitions of the C++ Run Time Library (from memory.h)
extern malloc(size_t s);
extern void free(void *p);

// Our own memory management functions
extern void *OurMalloc(size_t s);
extern void OurFree(void *p);

// Own variants to overrule malloc and free (instead of using #define)
template<typename T>
void *malloc(T t)
{
return OurMalloc(t);
}

template<typename T>
void free(T *t)
{
OurFree(t);
}

I tested this using the following code:

void main(void)
{
void *p = malloc(10);
free(p);
}

If I compile and run this, it seems that the call to malloc is correctly replaced by the templated variant. So far, so good.

However, the call to free is not replaced by the templated variant, and the standard C++ function is still called.

What rules does the C++ compiler use to decide which variant to give priority? Is this related to the Koenig-lookup rules?

Note: I tried this alternative because using #define does not solve the problem (see question http://stackoverflow.com/questions/2319381).

解决方案

Overload resolution is quite complicated in general.

In your case, it is quite easy: a function template is not considered if there is an exact match. For free it is the case (the standard free takes a void*), for malloc it isn't (the standard malloc takes a size_t, you are passing an int and size_t can't be a typedef for int -- size_t is unsigned). If you call free with a type other than void*, it should instantiate your template.

Running:

#include <iostream>

void* ml(size_t s)
{
    std::cout << "ml(size_t)\n";
}

void fr(void *p)
{
    std::cout << "fr(void*)\n";
}

template<typename T>
void* ml(T t)
{
    std::cout << "ml<" << typeid(T).name() << ">(T)\n";
}

template<typename T>
void fr(T *t)
{
    std::cout << "fr<" << typeid(T).name() << ">(T*)\n";
}

int main()
{
    void* p1 = ml((size_t)10);
    fr(p1);
    int* p2 = (int*)ml(10);
    fr(p2);
    return 0;
}

I get

ml(size_t)
fr(void*)
ml<i>(T)
fr<i>(T*)

and i is what returns typeid(int).name()

这篇关于在C ++中函数/方法/模板名称解析的首选项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆