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

Thread: Choosing between overloads

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

    Choosing between overloads

    I'm working on a project which I need to build with MSVC 2008. It uses a 3rd-party library which defines a struct which looks like this:-

    Code:
     template <typename Type, unsigned int StaticSize=8>
    struct hb_vector_t
    {
        // lots of other stuff
        inline Type * arrayZ (void) { return arrayZ_ ? arrayZ_ : static_array; }
    }
    }

    So to get the address of the array, a program would typically need to do something like this:-

    Code:
        memcpy (pages.arrayZ(), other->pages.arrayZ(), count * sizeof (pages.arrayZ()[0]));
    (where pages and other->pages are pointers to type hb_vector_t)

    Then some bright spark decided to simplify this by adding a couple of operators to hb_vector_t:-
    Code:
        template <typename T> inline operator  T * (void) { return arrayZ(); }
        template <typename T> inline operator const T * (void) const { return arrayZ(); }
    The intention was to simplify things by not needing to call '.arravZ() all the time - e.g.
    Code:
        memcpy (pages, other->pages, count * sizeof (pages[0]));
    Unfortunately - when I now try to compile the library with VS2008 it can't decide which operator to use and I see errors like this:-
    Code:
    error C2666: 'hb_vector_t<Type,StaticSize>::operator []' : 4 overloads have similar conversions
        : could be 'const hb_set_t::page_t &hb_vector_t<Type,StaticSize>::operator [](unsigned int) const'
        : or 'hb_set_t::page_t &hb_vector_t<Type,StaticSize>::operator [](unsigned int)'
        : or 'built-in C++ operator[(T *, int)'
        : or 'built-in C++ operator[(const T *, int)'
    Is there a way I could modify the call to memcpy() so that I can clarify which of the 4 overloads it needs to use?
    Last edited by John E; November 12th, 2018 at 07:28 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,509

    Re: Choosing between overloads

    Can you provide a self-contained test sample that exhibits this problem?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.3)

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

    Re: Choosing between overloads

    This isn't absolutely identical but it produces a very similar error (C2593, rather than C2666) :-

    Code:
    #include <memory.h>
    
    template <typename Type, unsigned int StaticSize=8>
    struct hb_vector_t
    {
    Type static_array[StaticSize];
    
    	inline Type * arrayZ (void) { return static_array; }
    	
    	template <typename T> inline operator  T * (void) { return arrayZ(); }
    	template <typename T> inline operator const T * (void) const { return arrayZ(); }
    };
    
    void SomeFunc ()
    {
    hb_vector_t<int, 1> pages;
    
    	memcpy (pages, pages, sizeof (pages[0]));
    }
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,509

    Re: Choosing between overloads

    Well the 'bad' news is that after supplying operator[] for the struct, this compiles OK with the latest version of VS2017! It even compiles OK back to VS2013 which is the earliest VS to which I have access. Sorry!

    Code:
    #include <memory.h>
    
    template <typename Type, unsigned int StaticSize = 8>
    struct hb_vector_t
    {
    	Type static_array[StaticSize];
    
    	inline Type * arrayZ(void) { return static_array; }
    
    	Type operator[](int e) { return static_array[e]; }
    
    	template <typename T> inline operator T * (void) { return arrayZ(); }
    	template <typename T> inline operator const T * (void) const { return arrayZ(); }
    };
    
    void SomeFunc()
    {
    	hb_vector_t<int, 1> pages;
    
    	memcpy(pages, pages, sizeof(pages[0]));
    }
    
    int main()
    {
    	SomeFunc();
    
    }
    Code:
    1>------ Rebuild All started: Project: test121, Configuration: Release Win32 ------
    1>test121.cpp
    1>Generating code
    1>Finished generating code
    1>test121.vcxproj -> c:\myprogs\test121.exe
    ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
    I suspect you need some casting on one or both of the first 2 params for memcpy().
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.3)

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

    Re: Choosing between overloads

    Thanks. The library's own developers test it with VS2015 and they haven't been able to reproduce it there either. Oh well...
    "A problem well stated is a problem half solved. - Charles F. Kettering

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,509

    Re: Choosing between overloads

    Just move from VS2008 to VS2017!

    VS2008 is c++98 - which together with MS's implementation had all sorts of quirks long forgotten thankfully! VS2015 is c++14 and there'll be loads of code that compiles OK with VS2015 but not with VS2008.
    Last edited by 2kaud; November 12th, 2018 at 12:18 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.3)

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

    Re: Choosing between overloads

    Interesting development here - if I try your example (slightly modified) it does compile correctly for me:-

    Code:
    #include <memory.h>
    
    template <typename Type, unsigned int StaticSize = 8>
    struct hb_vector_t
    {
    	Type static_array[StaticSize];
    
    	inline Type * arrayZ(void) { return static_array; }
    
    	Type operator[](int e) { return static_array[e]; }
    
    	template <typename T> inline operator T * (void) { return arrayZ(); }
    // Comment out this line --->	template <typename T> inline operator const T * (void) const { return arrayZ(); }
    };
    
    void SomeFunc()
    {
    	hb_vector_t<int, 1> pages;
    
    	memcpy(pages, pages, sizeof(pages[0]));
    }
    
    int main()
    {
    	SomeFunc();
    
    }
    The problem now is that I can't find a way of handling the const case. Restoring the commented line gives me this error:-

    Code:
    error C2662: 'hb_vector_t<Type,StaticSize>::arrayZ' : cannot convert 'this' pointer from 'const hb_vector_t<Type,StaticSize>' to 'hb_vector_t<Type,StaticSize> &'
            with
            [
                Type=int,
                StaticSize=1
            ]
            Conversion loses qualifiers
            .\Source.cpp(21) : see reference to function template instantiation 'hb_vector_t<Type,StaticSize>::operator T(void) const<void>' being compiled
            with
            [
                Type=int,
                StaticSize=1,
                T=void
            ]
    [Edit...] Changing that line seems to keep the compiler happy (though I'm not sure it'll still work as originally intended...)

    Code:
    Original line:
    	template <typename T> inline operator const T * (void) const { return arrayZ(); }
    
    Changed line:
    	template <typename T> inline operator const T * (void) { return arrayZ(); }
    Last edited by John E; November 13th, 2018 at 04:14 AM.
    "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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)