CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Threaded View

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    namespace expert needed :-(

    I'm building some code which has a lot of stuff in namespace Akupara. I've no idea what the word 'Akupara' actually means - but the namespace arrangement seems to be giving me a weird problem. Consider the following source modules (main.cpp / header_A.h / header_B.h). I've cut the code down to the minimum needed to reproduce the problem (the actual Akupara headers are quite a bit bigger than I've shown):-

    Code:
    // main.cpp
    #include "header_A.h"
    //#include "header_B.h"
    
    int main (int argc, char *argv[])
    {
    	return 0;
    }
    Code:
    // header_A.h
    #if !defined(_HEADER_A_INCLUDED_)
    #define _HEADER_A_INCLUDED_
    
    #ifndef __int8_t_defined
    #define __int8_t_defined
    typedef          __int8  int8_t;
    typedef unsigned __int8  uint8_t;
    typedef          __int16 int16_t;
    typedef unsigned __int16 uint16_t;
    typedef          __int32 int32_t;
    typedef unsigned __int32 uint32_t;
    typedef          __int64 int64_t;
    typedef unsigned __int64 uint64_t;
    #endif
    
    namespace Akupara
    {
    	namespace detail
    	{
    		template<unsigned int _size, bool _signed> 
    		struct integer_with_byte_count_base;
    
    		template<>
    		struct integer_with_byte_count_base<1,true> { typedef int8_t  type; };
    
    		template<>
    		struct integer_with_byte_count_base<2,true> { typedef int16_t type; };
    
    		template<>
    		struct integer_with_byte_count_base<4,true> { typedef int32_t type; };
    
    		template<>
    		struct integer_with_byte_count_base<8,true> { typedef int64_t type; };
    
    		template<>
    		struct integer_with_byte_count_base<1,false> { typedef uint8_t  type; };
    
    		template<>
    		struct integer_with_byte_count_base<2,false> { typedef uint16_t type; };
    
    		template<>
    		struct integer_with_byte_count_base<4,false> { typedef uint32_t type; };
    
    		template<>
    		struct integer_with_byte_count_base<8,false> { typedef uint64_t type; };
    	} // namespace detail
    
    	template<unsigned int _size, bool _signed=true>
    	struct integer_with_byte_count : public detail::integer_with_byte_count_base<_size,_signed>
    	{
    		typedef typename detail::integer_with_byte_count_base<_size,_signed>::type type;
    		static const bool s_correct_size = compiletime_assert<sizeof(type)==_size>::value;
    	};
    
    	template<unsigned int _size>
    	struct signed_integer_with_byte_count : public integer_with_byte_count<_size,true> {};
    
    	template<unsigned int _size>
    	struct unsigned_integer_with_byte_count : public integer_with_byte_count<_size,false> {};
    
    	template<typename _T, _T _v>
    	struct integral_constant
    	{
    		static const _T                    value = _v;
    		typedef _T                         value_type;
    		typedef integral_constant<_T, _v>  type;
    	}; // struct integral_constant
    	typedef integral_constant<bool, false> false_type;
        typedef integral_constant<bool, true > true_type;
    } // namespace Akupara
    
    #endif // _HEADER_A_INCLUDED_
    Code:
    // header_B.h
    #if !defined(_HEADER_B_INCLUDED_)
    #define _HEADER_B_INCLUDED_
    
    namespace Akupara
    {
        namespace threading
        {
            namespace atomic
            {
                namespace machine
                {
                    template<unsigned int _byte_count> struct implements_load  : public false_type {};
                    template<unsigned int _byte_count> struct implements_store : public false_type {};
                } // namespace machine
            } // namespace atomic
        } // namespace threading
    } // namespace Akupara
    
    
    namespace Akupara
    {
        namespace threading
        {
            namespace atomic
            {
                namespace detail
                {
                    template<unsigned int _byte_count> 
                    struct largest_atomic_byte_count_upto 
                    { 
                        static const unsigned int value = 
                            machine::implements_load<_byte_count>::value && machine::implements_store<_byte_count>::value ? _byte_count
    						: largest_atomic_byte_count_upto<_byte_count/2>::value; 
                    };
    
                    template<> 
                    struct largest_atomic_byte_count_upto<0> { static const unsigned int value = 0; };
    
                    const unsigned int k_byte_count_best_atomic = largest_atomic_byte_count_upto<sizeof(int)>::value;
                } // namespace detail
    
                typedef   signed_integer_with_byte_count< detail::k_byte_count_best_atomic >::type signed_integer_type;
                typedef unsigned_integer_with_byte_count< detail::k_byte_count_best_atomic >::type unsigned_integer_type;
                typedef signed_integer_type integer_type;
            } // namespace atomic
        } // namespace threading
    } // namespace Akupara
    
    #endif // _HEADER_B_INCLUDED_
    If I build main.cpp exactly as shown (i.e. with header_B.h commented out) it builds absolutely fine. However, if I uncomment the inclusion of header_B.h I suddenly get errors in header_A.h (even though header_A.h gets included before header_B.h!! As soon as I #include "header_B.h" I get this error at the red line in header_A.h:-

    error C2504: 'Akupara:etail::integer_with_byte_count_base<_size,_signed>' : base class undefined
    OTOH if I #include "header_B.h" - BUT - I comment out the blue lines, the error goes away. There must be some weird interaction between the two header files but I'm totally baffled. Is it something to do with namespaces?
    Last edited by John E; May 2nd, 2014 at 12:39 AM. Reason: typdefs added to the top of header_A.h
    "A problem well stated is a problem half solved.” - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured