通过ADL从另一个函数调用一个函数

Calling a function by ADL from another function

本文关键字:一个 函数 ADL 另一个 函数调用 通过      更新时间:2023-10-16

我有一个关于ADL在一般情况下如何发现类型的问题。具体来说,我有一些"通用"代码,我需要在编译时检查应该由ADL找到的函数的存在。例如:

#include "MyClass.h"
struct MyClass
{
    friend inline void DoSomething(MyClass& first, MyClass& second){} 
}
MyClass a, b;
DoSomething(a,b); //DoSomething in MyClass will be found by ADL

我有一个trait类,它使用'sizeof技巧'来检查这个ADL函数的存在:

//HasDoSomething.h
//type trait to check whether a type has a DoSomething function defined 
template<typename T>                                
struct has_doSomething
{                                                      
    typedef char yes;   
    typedef char (&no)[2];
    //SFINAE eliminates this when the type is invalid
    template <typename U, U> 
    struct Check; 
    template <typename U> 
    static yes Tester(Check<void(*)(U&, U&), &DoSomething>*);
    //overload resolution prefers anything at all over ...
    template <typename U> static no Tester(...);
    static bool const value = sizeof(Tester<T>(0)) == sizeof(yes);    
};  

trait class/sizeof技巧本身并不重要(如果您感兴趣,可以在《c++ Template Metaprogramming》一书中找到详细信息)。相反,问题在于除非我在定义了DoSomething(例如

)的(任意)类型的 #include之后#include它,否则该类型trait将无法编译。
#include "MyClass.h"
#include "HasDoSomething.h"

或者我用DoSomething函数声明创建一个虚拟类:

struct DummyClass
{
public:
    friend inline void DoSomething(DummyClass&, DummyClass&);
private:
    DummyClass(){}
};

并将其(直接或通过Dummy.h)包含到HasDoSomething.h中。通过强制#include的顺序或插入冗余代码来踢ADL查找似乎并不理想,所以我是否误解了什么或做错了什么?

ADL仅用于确定函数调用的重载集。
在编译器可以这样做之前,它必须首先确定这是一个函数调用,通过执行正常的名称查找并找到一个函数。