The compiler is only attempting to update the name table. The function is defined for every unique instantiation. This is obviously wrong.
Soma
Code:
#ifndef INT_H__2010_06_18_19_02
#define INT_H__2010_06_18_19_02
#include <limits>
typedef unsigned int uint;
namespace boost
{
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed long int32_t;
typedef signed long long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
typedef unsigned long long uint64_t;
}
template<bool Unsigned, uint Bits>
class Int;
template<bool Unsigned, uint Bits> struct Traits { static const bool Ok = false; };
template<> struct Traits<false, 8> { typedef boost::int8_t type; static const bool Ok = true; };
template<> struct Traits<false, 16> { typedef boost::int16_t type; static const bool Ok = true; };
template<> struct Traits<false, 32> { typedef boost::int32_t type; static const bool Ok = true; };
template<> struct Traits<false, 64> { typedef boost::int64_t type; static const bool Ok = true; };
template<> struct Traits<true, 8> { typedef boost::uint8_t type; static const bool Ok = true; };
template<> struct Traits<true, 16> { typedef boost::uint16_t type; static const bool Ok = true; };
template<> struct Traits<true, 32> { typedef boost::uint32_t type; static const bool Ok = true; };
template<> struct Traits<true, 64> { typedef boost::uint64_t type; static const bool Ok = true; };
template<typename T> struct IntFromT { typedef Int<!std::numeric_limits<T>::is_signed, sizeof(T) * 8> type; };
template<bool Unsigned = true, uint Bits = 32>
class Int
{
public:
typedef typename Traits<Unsigned, Bits>::type Int_t;
Int(Int_t integer): m_Int(integer) {}
Int(): m_Int(0) {}
template<bool Unsigned1, uint Bits1, typename T>
friend Int<Unsigned1, Bits1> operator + (const Int<Unsigned1, Bits1>& lhs, T rhs);
protected:
Int_t m_Int;
template<bool, uint> friend class Int;
};
template<bool Unsigned1, uint Bits1, typename T>
Int<Unsigned1, Bits1> operator + (const Int<Unsigned1, Bits1>& lhs, T rhs)
{
typename IntFromT<int>::type TmpRhs(rhs);
return lhs + TmpRhs;
}
#endif
int main()
{
Int<false, 8> MyInt;
Int<false, 16> MyInt2(10);
}