문제

사용하는 함수를 선언하는 데 문제가 있습니다. boost::enable_if: 다음 코드 조각은 컴파일러 오류를 제공합니다.

// Declaration
template <typename T>
void foo(T t);

// Definition
template <typename T>
typename boost::enable_if<boost::is_same<T, int> >::type foo(T t)
{
}

int main()
{
    foo(12);
    return 0;
}

컴파일 할 때 "Foo에 대한 모호한 호출"오류가 발생합니다. 정의에 따르면 enable_if, 'type'typedef는 다음과 같습니다 void 내가 볼 수있는 한, 조건이 사실 일 때 foo 성냥. 컴파일러가 왜 다르다고 생각하고 선언하는 올바른 방법이 있습니까? foo (바람직하게는 반복하지 않고 enable_if 부분)?

도움이 되었습니까?

해결책

이것은 enable_if의 문제 일뿐 만 아니라. Visual Studio 및 GCC에서 다음 코드로 동일한 오류가 발생합니다.

struct TypeVoid {
  typedef void type;
};

template<typename T>
void f();

template<typename T>
typename T::type f() {
}

int main()
{
  f<TypeVoid>();
  return 0;
}

주요 문제는 (인스턴스화 전) 리턴 유형이 템플릿 함수의 서명의 일부라는 것입니다. 더 많은 정보가 있습니다 여기.

코드와 관련하여, 선언이 정의를 지칭하는 경우, 당신은 다음과 같은 두 가지와 일치해야합니다.

// Declaration       
template <typename T>       
typename boost::enable_if<boost::is_same<T, int> >::type foo(T t);       

// Definition       
template <typename T>       
typename boost::enable_if<boost::is_same<T, int> >::type foo(T t)       
{       
}

선언이 다른 함수를 말하면 컴파일러는 올바른 기능을 선택할 수 없습니다. intS, 둘 다 유효하기 때문에. 그러나 첫 번째를 비활성화 할 수 있습니다 intS 사용 disable_if:

// Other function declaration
template <typename T>
typename boost::disable_if<boost::is_same<T, int> >::type foo(T t);

// Defition
template <typename T>       
typename boost::enable_if<boost::is_same<T, int> >::type foo(T t)       
{       
}

다른 팁

문제는 선언과 정의가 일치하지 않는다는 것입니다.

해결책은 선언에 정확히 동일한 서명이 포함되어야한다는 것입니다. enable_if 조금.

#include <boost/type_traits/is_same.hpp>
#include <boost/utility/enable_if.hpp>

// Declaration
template <typename T>
typename boost::enable_if<boost::is_same<T, int> >::type foo(T t);

// Definition
template <typename T>
typename boost::enable_if<boost::is_same<T, int> >::type foo(T t)
{
}

int main()
{
    foo(12);
    return 0;
}

이것은 VC2008에서 정상적으로 컴파일됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top