Template already instantiated

This is a discussion on Template already instantiated within the C++ Programming forums, part of the General Programming Boards category; OK, I'm out of ideas. Can anyone spot anything in this code? Error 1 error C2995: 'Int<Unsigned,Bits> operator +(const Int<Unsigned,Bits> ...

  1. #1
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604

    Template already instantiated

    OK, I'm out of ideas. Can anyone spot anything in this code?
    Error 1 error C2995: 'Int<Unsigned,Bits> operator +(const Int<Unsigned,Bits> &,T)' : function template has already been defined 75
    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)
    	{
    		typename IntFromT<int>::type TmpRhs(rhs);
    		return lhs + TmpRhs;
    	}
    
    protected:
    	Int_t m_Int;
    	template<bool, uint> friend class Int;
    };
    
    #endif
    
    int main()
    {
    	Int<false, 8> MyInt;
    	Int<false, 16> MyInt2(10);
    }
    Comeau also reports a compile error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,260
    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);
    }

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    What makes it different from normal member functions? Or better yet, what is a good resource to properly learn this stuff? What is the name of this "rule"?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,260
    What makes it different from normal member functions?
    O_o

    WTF!?

    It is different from a normal member function because it isn't a member function.

    Or better yet, what is a good resource to properly learn this stuff?
    Probably any of the eleven or twelve books I constantly recommend for learning C++.

    What is the name of this "rule"?
    The same "one definition" rule you should be used to.

    Soma

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Obviously there is a similar function for every instance, but since they have different types, they should be overloaded. Or, at least, this is from what I understand.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,260
    *tsk*

    I'm done.

    You'll have to search for the post made by CornedBee the last time you got confused about this issue or wait for someone else to answer it.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment issues
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2010, 11:55 AM
  2. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21