지능 운영자 < <가 (코드 참조) GCC 4.6.1 내가 오류 다음 얻고로 컴파일하는 동안 정의 된이 클래스 없습니다 : '연산자 < <'에 대한 일치의 'std :: cout < < a'. 무슨 일이야? Best_Fit의 모습누락 된 연산자 <<하지만이
template<class Int_T = int, typename Best_Fit<Int_T>::type Min_Range = std::numeric_limits<Int_T>::min(),
typename Best_Fit<Int_T>::type Max_Range = std::numeric_limits<Int_T>::max()>
class Int
{
Int_T data_;
Int_T get_data()const
{
return data_;
}
};
//Here is this operator defined
template<class Int_T>
std::ostream& operator<<(std::ostream& out, const Int<Int_T, Best_Fit<Int_T>::type, Best_Fit<Int_T>::type>& obj)
{
out << obj.get_data();
return out;
}
:
#ifndef BEST_FIT_H_INCLUDED
#define BEST_FIT_H_INCLUDED
struct Signed_Type
{
typedef long long type;
};
struct Unsigned_Type
{
typedef unsigned long long type;
};
template<bool Cond, class First, class Second>
struct if_
{
typedef typename First::type type;
};
template<class First, class Second>
struct if_<false,First,Second>
{
typedef typename Second::type type;
};
template<class Int_T>
struct Best_Fit
{//evaluate it lazily ;)
typedef typename if_<std::is_signed<Int_T>::value,Signed_Type,Unsigned_Type>::type type;
};
#endif // BEST_FIT_H_INCLUDED
편집 :
#include <iostream>
int main(int argc, char* argv[])
{
Int<signed char,1,20> a(30);
cout << a;
}
당신이 예와를 제공 할 수 있습니다 (운영자가 클래스 내부에 정의되어있는 경우 당신은 또한 외부를 선언하기로 결정 --unless 다음은 ADL을 통해 액세스 할 수 있다는 사실 등) 몇 가지 다른 차이가 있습니다 본관? –
@VJo 편집, OP – smallB
참조 그게 행복한 템플릿 마법입니다. 표준 숫자 형식을 래핑합니다. 그렇지 않습니까? 그것이 비밀이 아니라면, 당신의 목표는 무엇입니까? 왜 이런 짓을하는? – Septagram