CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Strange "incomplete type" compile error

    Hey guys. I am currently experimenting with some different ideas for a Unicode string class, and I came up with a problem I just can't figure out. I hope someone else here can.

    The following code has been tested on both VC++ 7.1, GCC 3.3 and Comeau (The online version), and none of them can compile it.
    Code:
    #include <iostream>
    #include <cstddef>
    
    struct utf8_tag {};
    struct utf16_tag {};
    struct utf32_tag {};
    
    namespace detail
        {
        typedef std::size_t size_type;
        typedef unsigned int value_type;
        }
    
    template<typename enc>
    struct encoding_traits;
    
    template<>
    struct encoding_traits<utf32_tag>
        {
        static detail::value_type get_at_index(detail::size_type index)
            {
            return index;
            }
        };
    
    class encoded_string
        {
        public:
            typedef detail::size_type size_type;
            typedef detail::value_type value_type;
               
        private:
            value_type (*get_at_index)(size_type);
    
        public:
            template<typename enc>
            encoded_string(enc dummy)
                {
                get_at_index = &encoding_traits<enc>::get_at_index;
                }
    
            value_type operator [] (size_type index)
                {
                return get_at_index(index);
                }
        };
    
    int main()
        {
        encoded_string test(utf32_tag());
    
        std::cout << test[1337] << std::endl;
    
        system("pause");
        }
    Basically all of them complain that the type of test is incomplete. (Although only Comeau is kind enough to let you know that's what the problem is.)
    Code:
    Comeau C/C++ 4.3.3 (Aug  6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
    Copyright 1988-2003 Comeau Computing.  All rights reserved.
    MODE:strict errors C++
    
    "ComeauTest.c", line 52: error: expression must be a pointer to a complete object
              type
          std::cout << test[1337] << std::endl;
                       ^
    
    1 error detected in the compilation of "ComeauTest.c".
    The really strange bit though, is that if I change "encoded_string test(utf32_tag());" to "encoded_string test = utf32_tag(); Everything suddenly compiles without a hitch.

    Anyone care to enlighten me?
    Insert entertaining phrase here

  2. #2
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: Strange "incomplete type" compile error

    VC++6 compiled it well (output is "1337").
    "Programs must be written for people to read, and only incidentally for machines to execute."

  3. #3
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: Strange "incomplete type" compile error

    Quote Originally Posted by RoboTact
    VC++6 compiled it well.
    Really? That was one compiler I really didn't expect to compile this. Still don't understand what the problem is though...
    Insert entertaining phrase here

  4. #4
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: Strange "incomplete type" compile error

    Experimenting a little more, I found out that making a template function that returns the encoding tag fixes the problem, but I still don't understand why the original code is wrong.
    Code:
    template<typename enc>
    enc make_tag()
        {
        return enc();
        }
    
    ...
    encoded_string test(make_tag<utf32_tag>());
    Insert entertaining phrase here

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Strange "incomplete type" compile error

    It also work without problems on VC++7.0 .
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Strange "incomplete type" compile error

    The only thing I can possibly imagine is that for a constructor template function, the rules are more explicit, and that the lookup rules as to resolving the template doesn't apply. The following gives no errors for Comeau C++.
    Code:
    #include <iostream>
    #include <cstddef>
    
    struct utf8_tag {};
    struct utf16_tag {};
    struct utf32_tag {};
    
    namespace detail
    {
        typedef std::size_t size_type;
        typedef unsigned int value_type;
    }
    
    template<typename enc>
    struct encoding_traits;
    
    template<>
    struct encoding_traits<utf32_tag>
    {
        static detail::value_type get_at_index(detail::size_type index)
        {
            return index;
        }
    };
    
    class encoded_string
    {
        public:
            typedef detail::size_type size_type;
            typedef detail::value_type value_type;
               
        private:
            value_type (*get_at_index)(size_type);
    
        public:
            encoded_string()
            {
            }
    
            template<typename enc>
             void init(enc dummy)
             {
                get_at_index = &encoding_traits<enc>::get_at_index;
             }
    
            value_type operator [] (size_type index)
                {
                return get_at_index(index);
                }
        };
    
    int main()
        {
        encoded_string test;
    
        test.init(utf32_tag());
    
        std::cout << test[1337] << std::endl;
    
        system("pause");
        }
    Note that all I did was to construct the object with a non-templated constructor, and then call a templated "init" function.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: Strange "incomplete type" compile error

    Quote Originally Posted by Paul McKenzie
    The only thing I can possibly imagine is that for a constructor template function, the rules are more explicit, and that the lookup rules as to resolving the template doesn't apply.
    Hmm.. I think you're on to something here.

    The problem seems to come from the fact that the only parameter to the constructor is templated. I tried making a version where the constructor has two parameters, the first of which has a known type (That is, not a templated one), and that works flawlessly in Comeau and VC++. It does however crash sometimes on GCC. (It does compile correctly... strange.)
    Code:
    template<typename enc>
    encoded_string(float first_dummy, enc dummy)
        {
        get_at_index = &encoding_traits<enc>::get_at_index;
        }
    
    ...
    
    encoded_string test(2.0f, utf32_tag());
    Guess I have to get myself a copy of the standard. There must be something about this in there, since this is consistent behaviour across multiple compilers.
    Insert entertaining phrase here

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