일반 상황에서 ADL이 형식을 찾는 방법과 관련된 질문이 있습니다. 특히 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
나는이 ADL 기능의 존재를 확인하기 '를 sizeof 트릭'을 사용하는 특성 클래스가 있습니다
이//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);
};
형질 클래스 /는 sizeof 자신을 속이는 것은 (중요하지 않다 C++ Template Metaprogramming 책에서 자세한 내용을 찾을 수 있습니다. 오히려, 문제는 이러한 유형의 특성은 양자 택일 내가 만들어 하나가 후 그것을 (임의)의의 #include 을 #include를하지 않으면 컴파일시 해봐요 정의한 않는 입력, 예를 들어,
#include "MyClass.h"
#include "HasDoSomething.h"
을 여부 것입니다 해봐요 함수 선언 더미 분류 :
struct DummyClass
{
public:
friend inline void DoSomething(DummyClass&, DummyClass&);
private:
DummyClass(){}
};
및 그 (직접 또는 Dummy.h 통해) HasDoSomething.h로 포함한다. #includes의 순서를 지정하거나 중복 코드를 삽입하여 ADL 조회를 실행해야하는 것이 이상적이지 않으므로 내가 잘못 이해했거나 잘못된 것이 있습니까?
죄송합니다. 그러나 & DoSomething은 오류 C2065를 제공합니다. 'DoSomething': 친구 함수로 DummyClass를 포함하지 않으면 선언되지 않은 식별자입니다. – TPJ